

<!DOCTYPE html>
<html lang="zh-CN" data-default-color-scheme=auto>



<head>
  <meta charset="UTF-8">
  <link rel="apple-touch-icon" sizes="76x76" href="/img/fluid.png">
  <link rel="icon" href="/img/fluid.png">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=5.0, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  
  <meta name="theme-color" content="#2f4154">
  <meta name="author" content="深海火锅店">
  <meta name="keywords" content="">
  
    <meta name="description" content="返回对象工具类123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960import java.io.Serializable;public class Result&lt;T&gt; implements Serializable &amp;">
<meta property="og:type" content="article">
<meta property="og:title" content="常用配置、工具类">
<meta property="og:url" content="http://example.com/2023/03/05/%E7%BC%96%E7%A8%8B%E8%AF%AD%E8%A8%80/Java/%E5%AD%A6%E4%B9%A0/%E5%9F%BA%E7%A1%80/008_%E5%B8%B8%E7%94%A8%E9%85%8D%E7%BD%AE%E3%80%81%E5%B7%A5%E5%85%B7%E7%B1%BB/index.html">
<meta property="og:site_name" content="Hexo">
<meta property="og:description" content="返回对象工具类123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960import java.io.Serializable;public class Result&lt;T&gt; implements Serializable &amp;">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://gitee.com/tongstyle/blog-img/raw/master/img/202303050935035.png">
<meta property="og:image" content="https://gitee.com/tongstyle/blog-img/raw/master/img/202303051728479.png">
<meta property="og:image" content="https://gitee.com/tongstyle/blog-img/raw/master/img/202303051728336.png">
<meta property="og:image" content="https://gitee.com/tongstyle/blog-img/raw/master/img/202303051729582.png">
<meta property="og:image" content="https://gitee.com/tongstyle/blog-img/raw/master/img/202303051729342.png">
<meta property="og:image" content="https://gitee.com/tongstyle/blog-img/raw/master/img/202303051712583.png">
<meta property="og:image" content="https://gitee.com/tongstyle/blog-img/raw/master/img/202303051714339.png">
<meta property="og:image" content="https://gitee.com/tongstyle/blog-img/raw/master/img/202303051714998.png">
<meta property="article:published_time" content="2023-03-05T10:00:00.000Z">
<meta property="article:modified_time" content="2024-02-15T16:04:19.051Z">
<meta property="article:author" content="深海火锅店">
<meta property="article:tag" content="常用配置、工具类">
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:image" content="https://gitee.com/tongstyle/blog-img/raw/master/img/202303050935035.png">
  
  
    <meta name="referrer" content="no-referrer-when-downgrade">
  
  
  <title>常用配置、工具类 - Hexo</title>

  <link  rel="stylesheet" href="https://lib.baomitu.com/twitter-bootstrap/4.6.1/css/bootstrap.min.css" />



  <link  rel="stylesheet" href="https://lib.baomitu.com/github-markdown-css/4.0.0/github-markdown.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/hint.css/2.7.0/hint.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.css" />



<!-- 主题依赖的图标库，不要自行修改 -->
<!-- Do not modify the link that theme dependent icons -->

<link rel="stylesheet" href="//at.alicdn.com/t/font_1749284_hj8rtnfg7um.css">



<link rel="stylesheet" href="//at.alicdn.com/t/font_1736178_lbnruvf0jn.css">


<link  rel="stylesheet" href="/css/main.css" />


  <link id="highlight-css" rel="stylesheet" href="/css/highlight.css" />
  
    <link id="highlight-css-dark" rel="stylesheet" href="/css/highlight-dark.css" />
  




  <script id="fluid-configs">
    var Fluid = window.Fluid || {};
    Fluid.ctx = Object.assign({}, Fluid.ctx)
    var CONFIG = {"hostname":"example.com","root":"/","version":"1.9.3","typing":{"enable":true,"typeSpeed":70,"cursorChar":"_","loop":false,"scope":[]},"anchorjs":{"enable":true,"element":"h1,h2,h3,h4,h5,h6","placement":"left","visible":"hover","icon":""},"progressbar":{"enable":true,"height_px":3,"color":"#29d","options":{"showSpinner":false,"trickleSpeed":100}},"code_language":{"enable":true,"default":"TEXT"},"copy_btn":true,"image_caption":{"enable":true},"image_zoom":{"enable":true,"img_url_replace":["",""]},"toc":{"enable":true,"placement":"right","headingSelector":"h1,h2,h3,h4,h5,h6","collapseDepth":0},"lazyload":{"enable":true,"loading_img":"/img/loading.gif","onlypost":false,"offset_factor":2},"web_analytics":{"enable":false,"follow_dnt":true,"baidu":null,"google":null,"gtag":null,"tencent":{"sid":null,"cid":null},"woyaola":null,"cnzz":null,"leancloud":{"app_id":null,"app_key":null,"server_url":null,"path":"window.location.pathname","ignore_local":false}},"search_path":"/local-search.xml"};

    if (CONFIG.web_analytics.follow_dnt) {
      var dntVal = navigator.doNotTrack || window.doNotTrack || navigator.msDoNotTrack;
      Fluid.ctx.dnt = dntVal && (dntVal.startsWith('1') || dntVal.startsWith('yes') || dntVal.startsWith('on'));
    }
  </script>
  <script  src="/js/utils.js" ></script>
  <script  src="/js/color-schema.js" ></script>
  


  
<meta name="generator" content="Hexo 6.3.0"></head>


<body>
  

  <header>
    

<div class="header-inner" style="height: 70vh;">
  <nav id="navbar" class="navbar fixed-top  navbar-expand-lg navbar-dark scrolling-navbar">
  <div class="container">
    <a class="navbar-brand" href="/">
      <strong>Fluid</strong>
    </a>

    <button id="navbar-toggler-btn" class="navbar-toggler" type="button" data-toggle="collapse"
            data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <div class="animated-icon"><span></span><span></span><span></span></div>
    </button>

    <!-- Collapsible content -->
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav ml-auto text-center">
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/">
                <i class="iconfont icon-home-fill"></i>
                首页
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/archives/">
                <i class="iconfont icon-archive-fill"></i>
                归档
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/categories/">
                <i class="iconfont icon-category-fill"></i>
                分类
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/tags/">
                <i class="iconfont icon-tags-fill"></i>
                标签
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/about/">
                <i class="iconfont icon-user-fill"></i>
                关于
              </a>
            </li>
          
        
        
          <li class="nav-item" id="search-btn">
            <a class="nav-link" target="_self" href="javascript:;" data-toggle="modal" data-target="#modalSearch" aria-label="Search">
              &nbsp;<i class="iconfont icon-search"></i>&nbsp;
            </a>
          </li>
          
        
        
          <li class="nav-item" id="color-toggle-btn">
            <a class="nav-link" target="_self" href="javascript:;" aria-label="Color Toggle">&nbsp;<i
                class="iconfont icon-dark" id="color-toggle-icon"></i>&nbsp;</a>
          </li>
        
      </ul>
    </div>
  </div>
</nav>

  

<div id="banner" class="banner" parallax=true
     style="background: url('/img/background.jpg') no-repeat center center; background-size: cover;">
  <div class="full-bg-img">
    <div class="mask flex-center" style="background-color: rgba(0, 0, 0, 0.3)">
      <div class="banner-text text-center fade-in-up">
        <div class="h2">
          
            <span id="subtitle" data-typed-text="常用配置、工具类"></span>
          
        </div>

        
          
  <div class="mt-3">
    
    
      <span class="post-meta">
        <i class="iconfont icon-date-fill" aria-hidden="true"></i>
        <time datetime="2023-03-05 18:00" pubdate>
          2023年3月5日 晚上
        </time>
      </span>
    
  </div>

  <div class="mt-1">
    
      <span class="post-meta mr-2">
        <i class="iconfont icon-chart"></i>
        
          37k 字
        
      </span>
    

    
      <span class="post-meta mr-2">
        <i class="iconfont icon-clock-fill"></i>
        
        
        
          307 分钟
        
      </span>
    

    
    
  </div>


        
      </div>

      
    </div>
  </div>
</div>

</div>

  </header>

  <main>
    
      

<div class="container-fluid nopadding-x">
  <div class="row nomargin-x">
    <div class="side-col d-none d-lg-block col-lg-2">
      

    </div>

    <div class="col-lg-8 nopadding-x-md">
      <div class="container nopadding-x-md" id="board-ctn">
        <div id="board">
          <article class="post-content mx-auto">
            <!-- SEO header -->
            <h1 style="display: none">常用配置、工具类</h1>
            
            
              <div class="markdown-body">
                
                <h2 id="返回对象工具类"><a href="#返回对象工具类" class="headerlink" title="返回对象工具类"></a>返回对象工具类</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">import</span> java.io.Serializable;<br><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">Result</span>&lt;T&gt; <span class="hljs-keyword">implements</span> <span class="hljs-title class_">Serializable</span> &#123;<span class="hljs-comment">//返回的结果集类</span><br>    <span class="hljs-comment">//服务器响应的状态   不是http状态</span><br>    <span class="hljs-keyword">private</span> Integer code;<br>    <span class="hljs-comment">//备注</span><br>    <span class="hljs-keyword">private</span> String msg;<br>    <span class="hljs-comment">//返回的数据</span><br>    <span class="hljs-keyword">private</span> T data;<br><br>    <span class="hljs-keyword">public</span> <span class="hljs-title function_">Result</span><span class="hljs-params">()</span> &#123;<br>    &#125;<br><br>    <span class="hljs-keyword">public</span> <span class="hljs-title function_">Result</span><span class="hljs-params">(Integer code, String msg, T data)</span> &#123;<br>        <span class="hljs-built_in">this</span>.code = code;<br>        <span class="hljs-built_in">this</span>.msg = msg;<br>        <span class="hljs-built_in">this</span>.data = data;<br>    &#125;<br><br>    <span class="hljs-keyword">public</span> Integer <span class="hljs-title function_">getCode</span><span class="hljs-params">()</span> &#123;<br>        <span class="hljs-keyword">return</span> code;<br>    &#125;<br><br>    <span class="hljs-keyword">public</span> Result&lt;?&gt; setCode(Integer code) &#123;<br>        <span class="hljs-built_in">this</span>.code = code;<br>        <span class="hljs-keyword">return</span> <span class="hljs-built_in">this</span>;<br>    &#125;<br><br>    <span class="hljs-keyword">public</span> String <span class="hljs-title function_">getMsg</span><span class="hljs-params">()</span> &#123;<br>        <span class="hljs-keyword">return</span> msg;<br>    &#125;<br><br>    <span class="hljs-keyword">public</span> Result&lt;?&gt; setMsg(String msg) &#123;<br>        <span class="hljs-built_in">this</span>.msg = msg;<br>        <span class="hljs-keyword">return</span> <span class="hljs-built_in">this</span>;<br>    &#125;<br><br>    <span class="hljs-keyword">public</span> T <span class="hljs-title function_">getData</span><span class="hljs-params">()</span> &#123;<br>        <span class="hljs-keyword">return</span> data;<br>    &#125;<br><br>    <span class="hljs-keyword">public</span> Result&lt;?&gt; setData(T data) &#123;<br>        <span class="hljs-built_in">this</span>.data = data;<br>        <span class="hljs-keyword">return</span> <span class="hljs-built_in">this</span>;<br>    &#125;<br><br>    <span class="hljs-meta">@Override</span><br>    <span class="hljs-keyword">public</span> String <span class="hljs-title function_">toString</span><span class="hljs-params">()</span> &#123;<br>        <span class="hljs-keyword">return</span> <span class="hljs-string">&quot;Result&#123;&quot;</span> +<br>                <span class="hljs-string">&quot;code=&quot;</span> + code +<br>                <span class="hljs-string">&quot;, msg=&#x27;&quot;</span> + msg + <span class="hljs-string">&#x27;\&#x27;&#x27;</span> +<br>                <span class="hljs-string">&quot;, data=&quot;</span> + data +<br>                <span class="hljs-string">&#x27;&#125;&#x27;</span>;<br>    &#125;<br><br>    <span class="hljs-keyword">public</span> Result&lt;?&gt; setCode(ResultEnum resultEnum) &#123;<br>        <span class="hljs-built_in">this</span>.code = resultEnum.code;<br>        <span class="hljs-keyword">return</span> <span class="hljs-built_in">this</span>;<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-comment">/**</span><br><span class="hljs-comment"> * 结果枚举</span><br><span class="hljs-comment"> *</span><br><span class="hljs-comment"> * <span class="hljs-doctag">@author</span> tong</span><br><span class="hljs-comment"> * <span class="hljs-doctag">@date</span> 2023/03/04</span><br><span class="hljs-comment"> */</span><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">enum</span> <span class="hljs-title class_">ResultEnum</span> &#123;<br>	<span class="hljs-comment">/**</span><br><span class="hljs-comment">	 * 成功</span><br><span class="hljs-comment">	 */</span><br>	SUCCESS(<span class="hljs-number">200</span>, <span class="hljs-string">&quot;操作成功&quot;</span>),<br>	<span class="hljs-comment">/**</span><br><span class="hljs-comment">	 * 对象创建成功</span><br><span class="hljs-comment">	 */</span><br>	CREATED(<span class="hljs-number">201</span>, <span class="hljs-string">&quot;对象创建成功&quot;</span>),<br>	<span class="hljs-comment">/**</span><br><span class="hljs-comment">	 * 请求已经被接受</span><br><span class="hljs-comment">	 */</span><br>	ACCEPTED(<span class="hljs-number">202</span>, <span class="hljs-string">&quot;请求已经被接受&quot;</span>),<br>	<span class="hljs-comment">/**</span><br><span class="hljs-comment">	 * 操作已经执行成功，但是没有返回数据</span><br><span class="hljs-comment">	 */</span><br>	NO_CONTENT(<span class="hljs-number">204</span>, <span class="hljs-string">&quot;操作已经执行成功，但是没有返回数据&quot;</span>),<br>	<span class="hljs-comment">/**</span><br><span class="hljs-comment">	 * 资源已被移除</span><br><span class="hljs-comment">	 */</span><br>	MOVED_PERM(<span class="hljs-number">301</span>, <span class="hljs-string">&quot;资源已被移除&quot;</span>),<br>	<span class="hljs-comment">/**</span><br><span class="hljs-comment">	 * 重定向</span><br><span class="hljs-comment">	 */</span><br>	SEE_OTHER(<span class="hljs-number">303</span>, <span class="hljs-string">&quot;重定向&quot;</span>),<br>	<span class="hljs-comment">/**</span><br><span class="hljs-comment">	 * 资源没有被修改</span><br><span class="hljs-comment">	 */</span><br>	NOT_MODIFIED(<span class="hljs-number">304</span>, <span class="hljs-string">&quot;资源没有被修改&quot;</span>),<br>	<span class="hljs-comment">/**</span><br><span class="hljs-comment">	 * 参数列表错误（缺少，格式不匹配）</span><br><span class="hljs-comment">	 */</span><br>	BAD_REQUEST(<span class="hljs-number">400</span>, <span class="hljs-string">&quot;操作失败&quot;</span>),<br>	<span class="hljs-comment">/**</span><br><span class="hljs-comment">	 * 未授权</span><br><span class="hljs-comment">	 */</span><br>	UNAUTHORIZED(<span class="hljs-number">401</span>, <span class="hljs-string">&quot;未授权&quot;</span>),<br>	<span class="hljs-comment">/**</span><br><span class="hljs-comment">	 * 访问受限，授权过期</span><br><span class="hljs-comment">	 */</span><br>	FORBIDDEN(<span class="hljs-number">403</span>, <span class="hljs-string">&quot;访问受限，授权过期&quot;</span>),<br>	<span class="hljs-comment">/**</span><br><span class="hljs-comment">	 * 资源、服务未找到</span><br><span class="hljs-comment">	 */</span><br>	NOT_FOUND(<span class="hljs-number">404</span>, <span class="hljs-string">&quot;资源，服务未找到&quot;</span>),<br>	<span class="hljs-comment">/**</span><br><span class="hljs-comment">	 * 不允许的http方法</span><br><span class="hljs-comment">	 */</span><br>	BAD_METHOD(<span class="hljs-number">405</span>, <span class="hljs-string">&quot;不允许的http方法&quot;</span>),<br>	<span class="hljs-comment">/**</span><br><span class="hljs-comment">	 * 资源冲突，或者资源被锁</span><br><span class="hljs-comment">	 */</span><br>	CONFLICT(<span class="hljs-number">409</span>, <span class="hljs-string">&quot;资源冲突，或者资源被锁&quot;</span>),<br>	<span class="hljs-comment">/**</span><br><span class="hljs-comment">	 * 不支持的数据、媒体类型</span><br><span class="hljs-comment">	 */</span><br>	UNSUPPORTED_TYPE(<span class="hljs-number">415</span>, <span class="hljs-string">&quot;不支持的数据、媒体类型&quot;</span>),<br>	<span class="hljs-comment">/**</span><br><span class="hljs-comment">	 * 服务器内部错误</span><br><span class="hljs-comment">	 */</span><br>	ERROR(<span class="hljs-number">500</span>, <span class="hljs-string">&quot;服务器内部错误&quot;</span>),<br>	<span class="hljs-comment">/**</span><br><span class="hljs-comment">	 * 接口未实现</span><br><span class="hljs-comment">	 */</span><br>	NOT_IMPLEMENTED(<span class="hljs-number">501</span>, <span class="hljs-string">&quot;接口未实现&quot;</span>);<br><br>	<span class="hljs-keyword">public</span> <span class="hljs-keyword">final</span> Integer code;<br>	<span class="hljs-keyword">public</span> <span class="hljs-keyword">final</span> String msg;<br><br>	ResultEnum(Integer code, String msg) &#123;<br>		<span class="hljs-built_in">this</span>.code = code;<br>		<span class="hljs-built_in">this</span>.msg = msg;<br>	&#125;<br><br>	<span class="hljs-keyword">public</span> Integer <span class="hljs-title function_">getCode</span><span class="hljs-params">()</span> &#123;<br>		<span class="hljs-keyword">return</span> code;<br>	&#125;<br><br>	<span class="hljs-keyword">public</span> String <span class="hljs-title function_">getMsg</span><span class="hljs-params">()</span> &#123;<br>		<span class="hljs-keyword">return</span> msg;<br>	&#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-comment">/**</span><br><span class="hljs-comment"> * 返回结果工具类</span><br><span class="hljs-comment"> *</span><br><span class="hljs-comment"> * <span class="hljs-doctag">@author</span> tong</span><br><span class="hljs-comment"> * <span class="hljs-doctag">@date</span> 2023/03/04</span><br><span class="hljs-comment"> */</span><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">ResultUtil</span> &#123;<br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> &lt;T&gt; Result&lt;T&gt;  <span class="hljs-title function_">defineSuccess</span><span class="hljs-params">(Integer code, T data)</span> &#123;<br>        <span class="hljs-type">Result</span> <span class="hljs-variable">result</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">Result</span>&lt;&gt;();<br>        <span class="hljs-keyword">return</span> result.setCode(code).setData(data);<br>    &#125;<br><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> &lt;T&gt; Result&lt;T&gt; <span class="hljs-title function_">success</span><span class="hljs-params">(T data)</span> &#123;<br>        <span class="hljs-type">Result</span> <span class="hljs-variable">result</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">Result</span>();<br>        result.setCode(ResultEnum.SUCCESS).setData(data);<br>        <span class="hljs-keyword">return</span> result;<br>    &#125;<br><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> &lt;T&gt; Result&lt;T&gt; <span class="hljs-title function_">fail</span><span class="hljs-params">(String msg)</span> &#123;<br>        <span class="hljs-type">Result</span> <span class="hljs-variable">result</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">Result</span>();<br>        result.setCode(ResultEnum.BAD_REQUEST).setMsg(msg);<br>        <span class="hljs-keyword">return</span> result;<br>    &#125;<br><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> &lt;T&gt; Result&lt;T&gt; <span class="hljs-title function_">defineFail</span><span class="hljs-params">(Integer code, String msg)</span>&#123;<br>        <span class="hljs-type">Result</span> <span class="hljs-variable">result</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">Result</span>();<br>        result.setCode(code).setMsg(msg);<br>        <span class="hljs-keyword">return</span> result;<br>    &#125;<br><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> &lt;T&gt; Result&lt;T&gt; <span class="hljs-title function_">define</span><span class="hljs-params">(Integer code, String msg, T data)</span>&#123;<br>        <span class="hljs-type">Result</span> <span class="hljs-variable">result</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">Result</span>();<br>        result.setCode(code).setMsg(msg).setData(data);<br>        <span class="hljs-keyword">return</span> result;<br>    &#125;<br><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> Object <span class="hljs-title function_">defineSuccess</span><span class="hljs-params">(ResultEnum success, Object list)</span> &#123;<br>        Result result=<span class="hljs-keyword">new</span> <span class="hljs-title class_">Result</span>();<br>        result.setCode(success);<br>        result.setData(list);<br>        <span class="hljs-keyword">return</span> result;<br>    &#125;<br><br>&#125;<br></code></pre></td></tr></table></figure>

<blockquote>
<p>用法</p>
</blockquote>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-meta">@RestController</span><br><span class="hljs-meta">@RequestMapping(&quot;user&quot;)</span><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">UserController</span> &#123;<br>	<span class="hljs-meta">@Autowired</span><br>	<span class="hljs-keyword">private</span> UserService userService;<br><br>	<span class="hljs-meta">@GetMapping(&quot;/id&quot;)</span><br>	<span class="hljs-keyword">public</span> Object <span class="hljs-title function_">getUser</span><span class="hljs-params">()</span> &#123;<br>		<span class="hljs-keyword">return</span> ResultUtil.define(<br>            ResultEnum.SUCCESS.getCode(), <br>            ResultEnum.SUCCESS.getMsg(), <br>            userService.list()<br>        );<br>	&#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<p><img src="https://gitee.com/tongstyle/blog-img/raw/master/img/202303050935035.png" srcset="/img/loading.gif" lazyload alt="image-20230305093542896"></p>
<blockquote>
<p>其他返回工具类</p>
</blockquote>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-meta">@Data</span><br><span class="hljs-meta">@AllArgsConstructor</span><br><span class="hljs-meta">@NoArgsConstructor</span><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">JsonData</span> &#123;<br>    <span class="hljs-comment">/**</span><br><span class="hljs-comment">     * 状态码 0 表示成功，1表示处理中，-1表示失败</span><br><span class="hljs-comment">     */</span><br>    <span class="hljs-keyword">private</span> Integer code;<br>    <span class="hljs-comment">/**</span><br><span class="hljs-comment">     * 数据</span><br><span class="hljs-comment">     */</span><br>    <span class="hljs-keyword">private</span> Object data;<br>    <span class="hljs-comment">/**</span><br><span class="hljs-comment">     * 描述</span><br><span class="hljs-comment">     */</span><br>    <span class="hljs-keyword">private</span> String msg;<br>    <span class="hljs-comment">// 成功，传入数据</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> JsonData <span class="hljs-title function_">buildSuccess</span><span class="hljs-params">()</span> &#123;<br>        <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">JsonData</span>(<span class="hljs-number">0</span>, <span class="hljs-literal">null</span>, <span class="hljs-literal">null</span>);<br>    &#125;<br>    <span class="hljs-comment">// 成功，传入数据</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> JsonData <span class="hljs-title function_">buildSuccess</span><span class="hljs-params">(Object data)</span> &#123;<br>        <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">JsonData</span>(<span class="hljs-number">0</span>, data, <span class="hljs-literal">null</span>);<br>    &#125;<br>    <span class="hljs-comment">// 失败，传入描述信息</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> JsonData <span class="hljs-title function_">buildError</span><span class="hljs-params">(String msg)</span> &#123;<br>        <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">JsonData</span>(-<span class="hljs-number">1</span>, <span class="hljs-literal">null</span>, msg);<br>    &#125;<br>    <span class="hljs-comment">// 失败，传入描述信息,状态码</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> JsonData <span class="hljs-title function_">buildError</span><span class="hljs-params">(String msg, Integer code)</span> &#123;<br>        <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">JsonData</span>(code, <span class="hljs-literal">null</span>, msg);<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-comment">/**</span><br><span class="hljs-comment"> * <span class="hljs-doctag">@Auther</span>: Administrator</span><br><span class="hljs-comment"> * <span class="hljs-doctag">@Date</span>: 2022/10/12/9:09</span><br><span class="hljs-comment"> * <span class="hljs-doctag">@Description</span>:</span><br><span class="hljs-comment"> */</span><br><br><span class="hljs-keyword">import</span> java.io.Serializable;<br><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">JsonResult</span>&lt;T&gt; <span class="hljs-keyword">implements</span> <span class="hljs-title class_">Serializable</span> &#123;<br>    <span class="hljs-keyword">private</span> Boolean success;<br>    <span class="hljs-keyword">private</span> Integer errorCode;<br>    <span class="hljs-keyword">private</span> String errorMsg;<br>    <span class="hljs-keyword">private</span> T data;<br><br>    <span class="hljs-keyword">public</span> <span class="hljs-title function_">JsonResult</span><span class="hljs-params">()</span> &#123;<br>    &#125;<br><br>    <span class="hljs-keyword">public</span> <span class="hljs-title function_">JsonResult</span><span class="hljs-params">(<span class="hljs-type">boolean</span> success)</span> &#123;<br>        <span class="hljs-built_in">this</span>.success = success;<br>        <span class="hljs-built_in">this</span>.errorCode = success ? com.example.demo.util.ResultCode.SUCCESS.getCode() : com.example.demo.util.ResultCode.COMMON_FAIL.getCode();<br>        <span class="hljs-built_in">this</span>.errorMsg = success ? com.example.demo.util.ResultCode.SUCCESS.getMessage() : com.example.demo.util.ResultCode.COMMON_FAIL.getMessage();<br>    &#125;<br><br>    <span class="hljs-keyword">public</span> <span class="hljs-title function_">JsonResult</span><span class="hljs-params">(<span class="hljs-type">boolean</span> success, com.example.demo.util.ResultCode resultEnum)</span> &#123;<br>        <span class="hljs-built_in">this</span>.success = success;<br>        <span class="hljs-built_in">this</span>.errorCode = success ? com.example.demo.util.ResultCode.SUCCESS.getCode() : (resultEnum == <span class="hljs-literal">null</span> ? com.example.demo.util.ResultCode.COMMON_FAIL.getCode() : resultEnum.getCode());<br>        <span class="hljs-built_in">this</span>.errorMsg = success ? com.example.demo.util.ResultCode.SUCCESS.getMessage() : (resultEnum == <span class="hljs-literal">null</span> ? com.example.demo.util.ResultCode.COMMON_FAIL.getMessage() : resultEnum.getMessage());<br>    &#125;<br><br>    <span class="hljs-keyword">public</span> <span class="hljs-title function_">JsonResult</span><span class="hljs-params">(<span class="hljs-type">boolean</span> success, T data)</span> &#123;<br>        <span class="hljs-built_in">this</span>.success = success;<br>        <span class="hljs-built_in">this</span>.errorCode = success ? com.example.demo.util.ResultCode.SUCCESS.getCode() : com.example.demo.util.ResultCode.COMMON_FAIL.getCode();<br>        <span class="hljs-built_in">this</span>.errorMsg = success ? com.example.demo.util.ResultCode.SUCCESS.getMessage() : com.example.demo.util.ResultCode.COMMON_FAIL.getMessage();<br>        <span class="hljs-built_in">this</span>.data = data;<br>    &#125;<br><br>    <span class="hljs-keyword">public</span> <span class="hljs-title function_">JsonResult</span><span class="hljs-params">(<span class="hljs-type">boolean</span> success, com.example.demo.util.ResultCode resultEnum, T data)</span> &#123;<br>        <span class="hljs-built_in">this</span>.success = success;<br>        <span class="hljs-built_in">this</span>.errorCode = success ? com.example.demo.util.ResultCode.SUCCESS.getCode() : (resultEnum == <span class="hljs-literal">null</span> ? com.example.demo.util.ResultCode.COMMON_FAIL.getCode() : resultEnum.getCode());<br>        <span class="hljs-built_in">this</span>.errorMsg = success ? com.example.demo.util.ResultCode.SUCCESS.getMessage() : (resultEnum == <span class="hljs-literal">null</span> ? com.example.demo.util.ResultCode.COMMON_FAIL.getMessage() : resultEnum.getMessage());<br>        <span class="hljs-built_in">this</span>.data = data;<br>    &#125;<br><br>    <span class="hljs-keyword">public</span> Boolean <span class="hljs-title function_">getSuccess</span><span class="hljs-params">()</span> &#123;<br>        <span class="hljs-keyword">return</span> success;<br>    &#125;<br><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">setSuccess</span><span class="hljs-params">(Boolean success)</span> &#123;<br>        <span class="hljs-built_in">this</span>.success = success;<br>    &#125;<br><br>    <span class="hljs-keyword">public</span> Integer <span class="hljs-title function_">getErrorCode</span><span class="hljs-params">()</span> &#123;<br>        <span class="hljs-keyword">return</span> errorCode;<br>    &#125;<br><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">setErrorCode</span><span class="hljs-params">(Integer errorCode)</span> &#123;<br>        <span class="hljs-built_in">this</span>.errorCode = errorCode;<br>    &#125;<br><br>    <span class="hljs-keyword">public</span> String <span class="hljs-title function_">getErrorMsg</span><span class="hljs-params">()</span> &#123;<br>        <span class="hljs-keyword">return</span> errorMsg;<br>    &#125;<br><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">setErrorMsg</span><span class="hljs-params">(String errorMsg)</span> &#123;<br>        <span class="hljs-built_in">this</span>.errorMsg = errorMsg;<br>    &#125;<br><br>    <span class="hljs-keyword">public</span> T <span class="hljs-title function_">getData</span><span class="hljs-params">()</span> &#123;<br>        <span class="hljs-keyword">return</span> data;<br>    &#125;<br><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">setData</span><span class="hljs-params">(T data)</span> &#123;<br>        <span class="hljs-built_in">this</span>.data = data;<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-comment">/**</span><br><span class="hljs-comment"> * <span class="hljs-doctag">@Auther</span>: Administrator</span><br><span class="hljs-comment"> * <span class="hljs-doctag">@Date</span>: 2022/10/12/9:09</span><br><span class="hljs-comment"> * <span class="hljs-doctag">@Description</span>:</span><br><span class="hljs-comment"> */</span><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">enum</span> <span class="hljs-title class_">ResultCode</span> &#123;<br>    <span class="hljs-comment">/* 成功 */</span><br>    SUCCESS(<span class="hljs-number">200</span>, <span class="hljs-string">&quot;成功&quot;</span>),<br><br>    <span class="hljs-comment">/* 默认失败 */</span><br>    COMMON_FAIL(<span class="hljs-number">999</span>, <span class="hljs-string">&quot;失败&quot;</span>),<br><br>    <span class="hljs-comment">/* 参数错误：1000～1999 */</span><br>    PARAM_NOT_VALID(<span class="hljs-number">1001</span>, <span class="hljs-string">&quot;参数无效&quot;</span>),<br>    PARAM_IS_BLANK(<span class="hljs-number">1002</span>, <span class="hljs-string">&quot;参数为空&quot;</span>),<br>    PARAM_TYPE_ERROR(<span class="hljs-number">1003</span>, <span class="hljs-string">&quot;参数类型错误&quot;</span>),<br>    PARAM_NOT_COMPLETE(<span class="hljs-number">1004</span>, <span class="hljs-string">&quot;参数缺失&quot;</span>),<br><br>    <span class="hljs-comment">/* 用户错误 */</span><br>    USER_NOT_LOGIN(<span class="hljs-number">2001</span>, <span class="hljs-string">&quot;用户未登录&quot;</span>),<br>    USER_ACCOUNT_EXPIRED(<span class="hljs-number">2002</span>, <span class="hljs-string">&quot;账号已过期&quot;</span>),<br>    USER_CREDENTIALS_ERROR(<span class="hljs-number">2003</span>, <span class="hljs-string">&quot;密码错误&quot;</span>),<br>    USER_CREDENTIALS_EXPIRED(<span class="hljs-number">2004</span>, <span class="hljs-string">&quot;密码过期&quot;</span>),<br>    USER_ACCOUNT_DISABLE(<span class="hljs-number">2005</span>, <span class="hljs-string">&quot;账号不可用&quot;</span>),<br>    USER_ACCOUNT_LOCKED(<span class="hljs-number">2006</span>, <span class="hljs-string">&quot;账号被锁定&quot;</span>),<br>    USER_ACCOUNT_NOT_EXIST(<span class="hljs-number">2007</span>, <span class="hljs-string">&quot;账号不存在&quot;</span>),<br>    USER_ACCOUNT_ALREADY_EXIST(<span class="hljs-number">2008</span>, <span class="hljs-string">&quot;账号已存在&quot;</span>),<br>    USER_ACCOUNT_USE_BY_OTHERS(<span class="hljs-number">2009</span>, <span class="hljs-string">&quot;账号下线&quot;</span>),<br><br>    <span class="hljs-comment">/* 业务错误 */</span><br>    NO_PERMISSION(<span class="hljs-number">3001</span>, <span class="hljs-string">&quot;没有权限&quot;</span>);<br>    <span class="hljs-keyword">private</span> Integer code;<br>    <span class="hljs-keyword">private</span> String message;<br><br>    ResultCode(Integer code, String message) &#123;<br>        <span class="hljs-built_in">this</span>.code = code;<br>        <span class="hljs-built_in">this</span>.message = message;<br>    &#125;<br><br>    <span class="hljs-keyword">public</span> Integer <span class="hljs-title function_">getCode</span><span class="hljs-params">()</span> &#123;<br>        <span class="hljs-keyword">return</span> code;<br>    &#125;<br><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">setCode</span><span class="hljs-params">(Integer code)</span> &#123;<br>        <span class="hljs-built_in">this</span>.code = code;<br>    &#125;<br><br>    <span class="hljs-keyword">public</span> String <span class="hljs-title function_">getMessage</span><span class="hljs-params">()</span> &#123;<br>        <span class="hljs-keyword">return</span> message;<br>    &#125;<br><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">setMessage</span><span class="hljs-params">(String message)</span> &#123;<br>        <span class="hljs-built_in">this</span>.message = message;<br>    &#125;<br><br>    <span class="hljs-comment">/**</span><br><span class="hljs-comment">     * 根据code获取message</span><br><span class="hljs-comment">     *</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> code</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@return</span></span><br><span class="hljs-comment">     */</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> String <span class="hljs-title function_">getMessageByCode</span><span class="hljs-params">(Integer code)</span> &#123;<br>        <span class="hljs-keyword">for</span> (ResultCode ele : values()) &#123;<br>            <span class="hljs-keyword">if</span> (ele.getCode().equals(code)) &#123;<br>                <span class="hljs-keyword">return</span> ele.getMessage();<br>            &#125;<br>        &#125;<br>        <span class="hljs-keyword">return</span> <span class="hljs-literal">null</span>;<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-comment">/**</span><br><span class="hljs-comment"> * <span class="hljs-doctag">@Auther</span>: Administrator</span><br><span class="hljs-comment"> * <span class="hljs-doctag">@Date</span>: 2022/10/12/9:10</span><br><span class="hljs-comment"> * <span class="hljs-doctag">@Description</span>:</span><br><span class="hljs-comment"> */</span><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">ResultTool</span> &#123;<br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> com.example.demo.util.JsonResult <span class="hljs-title function_">success</span><span class="hljs-params">()</span> &#123;<br>        <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">com</span>.example.demo.util.JsonResult(<span class="hljs-literal">true</span>);<br>    &#125;<br><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> &lt;T&gt; com.example.demo.util.JsonResult&lt;T&gt; <span class="hljs-title function_">success</span><span class="hljs-params">(T data)</span> &#123;<br>        <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">com</span>.example.demo.util.JsonResult(<span class="hljs-literal">true</span>, data);<br>    &#125;<br><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> com.example.demo.util.JsonResult <span class="hljs-title function_">fail</span><span class="hljs-params">()</span> &#123;<br>        <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">com</span>.example.demo.util.JsonResult(<span class="hljs-literal">false</span>);<br>    &#125;<br><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> com.example.demo.util.JsonResult <span class="hljs-title function_">fail</span><span class="hljs-params">(com.example.demo.util.ResultCode resultEnum)</span> &#123;<br>        <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">com</span>.example.demo.util.JsonResult(<span class="hljs-literal">false</span>, resultEnum);<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>



<h2 id="Redis配置及工具类"><a href="#Redis配置及工具类" class="headerlink" title="Redis配置及工具类"></a>Redis配置及工具类</h2><h3 id="Redis配置"><a href="#Redis配置" class="headerlink" title="Redis配置"></a>Redis配置</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br></pre></td><td class="code"><pre><code class="hljs java"><br><span class="hljs-keyword">import</span> com.alibaba.fastjson.JSON;<br><span class="hljs-keyword">import</span> com.alibaba.fastjson.serializer.SerializerFeature;<br><span class="hljs-keyword">import</span> com.fasterxml.jackson.databind.JavaType;<br><span class="hljs-keyword">import</span> com.fasterxml.jackson.databind.ObjectMapper;<br><span class="hljs-keyword">import</span> com.fasterxml.jackson.databind.type.TypeFactory;<br><span class="hljs-keyword">import</span> org.springframework.data.redis.serializer.RedisSerializer;<br><span class="hljs-keyword">import</span> org.springframework.data.redis.serializer.SerializationException;<br><span class="hljs-keyword">import</span> com.alibaba.fastjson.parser.ParserConfig;<br><span class="hljs-keyword">import</span> org.springframework.util.Assert;<br><span class="hljs-keyword">import</span> java.nio.charset.Charset;<br><br><span class="hljs-comment">/**</span><br><span class="hljs-comment"> * Redis使用FastJson序列化</span><br><span class="hljs-comment"> * </span><br><span class="hljs-comment"> * <span class="hljs-doctag">@author</span> sg</span><br><span class="hljs-comment"> */</span><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">FastJsonRedisSerializer</span>&lt;T&gt; <span class="hljs-keyword">implements</span> <span class="hljs-title class_">RedisSerializer</span>&lt;T&gt;<br>&#123;<br><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">final</span> <span class="hljs-type">Charset</span> <span class="hljs-variable">DEFAULT_CHARSET</span> <span class="hljs-operator">=</span> Charset.forName(<span class="hljs-string">&quot;UTF-8&quot;</span>);<br><br>    <span class="hljs-keyword">private</span> Class&lt;T&gt; clazz;<br><br>    <span class="hljs-keyword">static</span><br>    &#123;<br>        ParserConfig.getGlobalInstance().setAutoTypeSupport(<span class="hljs-literal">true</span>);<br>    &#125;<br><br>    <span class="hljs-keyword">public</span> <span class="hljs-title function_">FastJsonRedisSerializer</span><span class="hljs-params">(Class&lt;T&gt; clazz)</span><br>    &#123;<br>        <span class="hljs-built_in">super</span>();<br>        <span class="hljs-built_in">this</span>.clazz = clazz;<br>    &#125;<br><br>    <span class="hljs-meta">@Override</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-type">byte</span>[] serialize(T t) <span class="hljs-keyword">throws</span> SerializationException<br>    &#123;<br>        <span class="hljs-keyword">if</span> (t == <span class="hljs-literal">null</span>)<br>        &#123;<br>            <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">byte</span>[<span class="hljs-number">0</span>];<br>        &#125;<br>        <span class="hljs-keyword">return</span> JSON.toJSONString(t, SerializerFeature.WriteClassName).getBytes(DEFAULT_CHARSET);<br>    &#125;<br><br>    <span class="hljs-meta">@Override</span><br>    <span class="hljs-keyword">public</span> T <span class="hljs-title function_">deserialize</span><span class="hljs-params">(<span class="hljs-type">byte</span>[] bytes)</span> <span class="hljs-keyword">throws</span> SerializationException<br>    &#123;<br>        <span class="hljs-keyword">if</span> (bytes == <span class="hljs-literal">null</span> || bytes.length &lt;= <span class="hljs-number">0</span>)<br>        &#123;<br>            <span class="hljs-keyword">return</span> <span class="hljs-literal">null</span>;<br>        &#125;<br>        <span class="hljs-type">String</span> <span class="hljs-variable">str</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">String</span>(bytes, DEFAULT_CHARSET);<br><br>        <span class="hljs-keyword">return</span> JSON.parseObject(str, clazz);<br>    &#125;<br><br><br>    <span class="hljs-keyword">protected</span> JavaType <span class="hljs-title function_">getJavaType</span><span class="hljs-params">(Class&lt;?&gt; clazz)</span><br>    &#123;<br>        <span class="hljs-keyword">return</span> TypeFactory.defaultInstance().constructType(clazz);<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><code class="hljs java"><br><span class="hljs-keyword">import</span> org.springframework.context.annotation.Bean;<br><span class="hljs-keyword">import</span> org.springframework.context.annotation.Configuration;<br><span class="hljs-keyword">import</span> org.springframework.data.redis.connection.RedisConnectionFactory;<br><span class="hljs-keyword">import</span> org.springframework.data.redis.core.RedisTemplate;<br><span class="hljs-keyword">import</span> org.springframework.data.redis.serializer.StringRedisSerializer;<br><br><span class="hljs-meta">@Configuration</span><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">RedisConfig</span> &#123;<br><br>    <span class="hljs-meta">@Bean</span><br>    <span class="hljs-meta">@SuppressWarnings(value = &#123; &quot;unchecked&quot;, &quot;rawtypes&quot; &#125;)</span><br>    <span class="hljs-keyword">public</span> RedisTemplate&lt;Object, Object&gt; <span class="hljs-title function_">redisTemplate</span><span class="hljs-params">(RedisConnectionFactory connectionFactory)</span><br>    &#123;<br>        RedisTemplate&lt;Object, Object&gt; template = <span class="hljs-keyword">new</span> <span class="hljs-title class_">RedisTemplate</span>&lt;&gt;();<br>        template.setConnectionFactory(connectionFactory);<br><br>        <span class="hljs-type">FastJsonRedisSerializer</span> <span class="hljs-variable">serializer</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">FastJsonRedisSerializer</span>(Object.class);<br><br>        <span class="hljs-comment">// 使用StringRedisSerializer来序列化和反序列化redis的key值</span><br>        template.setKeySerializer(<span class="hljs-keyword">new</span> <span class="hljs-title class_">StringRedisSerializer</span>());<br>        template.setValueSerializer(serializer);<br><br>        <span class="hljs-comment">// Hash的key也采用StringRedisSerializer的序列化方式</span><br>        template.setHashKeySerializer(<span class="hljs-keyword">new</span> <span class="hljs-title class_">StringRedisSerializer</span>());<br>        template.setHashValueSerializer(serializer);<br><br>        template.afterPropertiesSet();<br>        <span class="hljs-keyword">return</span> template;<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>
<h3 id="Redis工具类"><a href="#Redis工具类" class="headerlink" title="Redis工具类"></a>Redis工具类</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br><span class="line">172</span><br><span class="line">173</span><br><span class="line">174</span><br><span class="line">175</span><br><span class="line">176</span><br><span class="line">177</span><br><span class="line">178</span><br><span class="line">179</span><br><span class="line">180</span><br><span class="line">181</span><br><span class="line">182</span><br><span class="line">183</span><br><span class="line">184</span><br><span class="line">185</span><br><span class="line">186</span><br><span class="line">187</span><br><span class="line">188</span><br><span class="line">189</span><br><span class="line">190</span><br><span class="line">191</span><br><span class="line">192</span><br><span class="line">193</span><br><span class="line">194</span><br><span class="line">195</span><br><span class="line">196</span><br><span class="line">197</span><br><span class="line">198</span><br><span class="line">199</span><br><span class="line">200</span><br><span class="line">201</span><br><span class="line">202</span><br><span class="line">203</span><br><span class="line">204</span><br><span class="line">205</span><br><span class="line">206</span><br><span class="line">207</span><br><span class="line">208</span><br><span class="line">209</span><br><span class="line">210</span><br><span class="line">211</span><br><span class="line">212</span><br><span class="line">213</span><br><span class="line">214</span><br><span class="line">215</span><br><span class="line">216</span><br><span class="line">217</span><br><span class="line">218</span><br><span class="line">219</span><br><span class="line">220</span><br><span class="line">221</span><br><span class="line">222</span><br><span class="line">223</span><br><span class="line">224</span><br><span class="line">225</span><br><span class="line">226</span><br><span class="line">227</span><br><span class="line">228</span><br><span class="line">229</span><br><span class="line">230</span><br></pre></td><td class="code"><pre><code class="hljs java"><br><span class="hljs-keyword">import</span> java.util.*;<br><span class="hljs-keyword">import</span> java.util.concurrent.TimeUnit;<br><br><span class="hljs-meta">@SuppressWarnings(value = &#123; &quot;unchecked&quot;, &quot;rawtypes&quot; &#125;)</span><br><span class="hljs-meta">@Component</span><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">RedisCache</span><br>&#123;<br>    <span class="hljs-meta">@Autowired</span><br>    <span class="hljs-keyword">public</span> RedisTemplate redisTemplate;<br><br>    <span class="hljs-comment">/**</span><br><span class="hljs-comment">     * 缓存基本的对象，Integer、String、实体类等</span><br><span class="hljs-comment">     *</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> key 缓存的键值</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> value 缓存的值</span><br><span class="hljs-comment">     */</span><br>    <span class="hljs-keyword">public</span> &lt;T&gt; <span class="hljs-keyword">void</span> <span class="hljs-title function_">setCacheObject</span><span class="hljs-params">(<span class="hljs-keyword">final</span> String key, <span class="hljs-keyword">final</span> T value)</span><br>    &#123;<br>        redisTemplate.opsForValue().set(key, value);<br>    &#125;<br><br>    <span class="hljs-comment">/**</span><br><span class="hljs-comment">     * 缓存基本的对象，Integer、String、实体类等</span><br><span class="hljs-comment">     *</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> key 缓存的键值</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> value 缓存的值</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> timeout 时间</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> timeUnit 时间颗粒度</span><br><span class="hljs-comment">     */</span><br>    <span class="hljs-keyword">public</span> &lt;T&gt; <span class="hljs-keyword">void</span> <span class="hljs-title function_">setCacheObject</span><span class="hljs-params">(<span class="hljs-keyword">final</span> String key, <span class="hljs-keyword">final</span> T value, <span class="hljs-keyword">final</span> Integer timeout, <span class="hljs-keyword">final</span> TimeUnit timeUnit)</span><br>    &#123;<br>        redisTemplate.opsForValue().set(key, value, timeout, timeUnit);<br>    &#125;<br><br>    <span class="hljs-comment">/**</span><br><span class="hljs-comment">     * 设置有效时间</span><br><span class="hljs-comment">     *</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> key Redis键</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> timeout 超时时间</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@return</span> true=设置成功；false=设置失败</span><br><span class="hljs-comment">     */</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-type">boolean</span> <span class="hljs-title function_">expire</span><span class="hljs-params">(<span class="hljs-keyword">final</span> String key, <span class="hljs-keyword">final</span> <span class="hljs-type">long</span> timeout)</span><br>    &#123;<br>        <span class="hljs-keyword">return</span> expire(key, timeout, TimeUnit.SECONDS);<br>    &#125;<br><br>    <span class="hljs-comment">/**</span><br><span class="hljs-comment">     * 设置有效时间</span><br><span class="hljs-comment">     *</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> key Redis键</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> timeout 超时时间</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> unit 时间单位</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@return</span> true=设置成功；false=设置失败</span><br><span class="hljs-comment">     */</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-type">boolean</span> <span class="hljs-title function_">expire</span><span class="hljs-params">(<span class="hljs-keyword">final</span> String key, <span class="hljs-keyword">final</span> <span class="hljs-type">long</span> timeout, <span class="hljs-keyword">final</span> TimeUnit unit)</span><br>    &#123;<br>        <span class="hljs-keyword">return</span> redisTemplate.expire(key, timeout, unit);<br>    &#125;<br><br>    <span class="hljs-comment">/**</span><br><span class="hljs-comment">     * 获得缓存的基本对象。</span><br><span class="hljs-comment">     *</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> key 缓存键值</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@return</span> 缓存键值对应的数据</span><br><span class="hljs-comment">     */</span><br>    <span class="hljs-keyword">public</span> &lt;T&gt; T <span class="hljs-title function_">getCacheObject</span><span class="hljs-params">(<span class="hljs-keyword">final</span> String key)</span><br>    &#123;<br>        ValueOperations&lt;String, T&gt; operation = redisTemplate.opsForValue();<br>        <span class="hljs-keyword">return</span> operation.get(key);<br>    &#125;<br><br>    <span class="hljs-comment">/**</span><br><span class="hljs-comment">     * 删除单个对象</span><br><span class="hljs-comment">     *</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> key</span><br><span class="hljs-comment">     */</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-type">boolean</span> <span class="hljs-title function_">deleteObject</span><span class="hljs-params">(<span class="hljs-keyword">final</span> String key)</span><br>    &#123;<br>        <span class="hljs-keyword">return</span> redisTemplate.delete(key);<br>    &#125;<br><br>    <span class="hljs-comment">/**</span><br><span class="hljs-comment">     * 删除集合对象</span><br><span class="hljs-comment">     *</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> collection 多个对象</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@return</span></span><br><span class="hljs-comment">     */</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-type">long</span> <span class="hljs-title function_">deleteObject</span><span class="hljs-params">(<span class="hljs-keyword">final</span> Collection collection)</span><br>    &#123;<br>        <span class="hljs-keyword">return</span> redisTemplate.delete(collection);<br>    &#125;<br><br>    <span class="hljs-comment">/**</span><br><span class="hljs-comment">     * 缓存List数据</span><br><span class="hljs-comment">     *</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> key 缓存的键值</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> dataList 待缓存的List数据</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@return</span> 缓存的对象</span><br><span class="hljs-comment">     */</span><br>    <span class="hljs-keyword">public</span> &lt;T&gt; <span class="hljs-type">long</span> <span class="hljs-title function_">setCacheList</span><span class="hljs-params">(<span class="hljs-keyword">final</span> String key, <span class="hljs-keyword">final</span> List&lt;T&gt; dataList)</span><br>    &#123;<br>        <span class="hljs-type">Long</span> <span class="hljs-variable">count</span> <span class="hljs-operator">=</span> redisTemplate.opsForList().rightPushAll(key, dataList);<br>        <span class="hljs-keyword">return</span> count == <span class="hljs-literal">null</span> ? <span class="hljs-number">0</span> : count;<br>    &#125;<br><br>    <span class="hljs-comment">/**</span><br><span class="hljs-comment">     * 获得缓存的list对象</span><br><span class="hljs-comment">     *</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> key 缓存的键值</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@return</span> 缓存键值对应的数据</span><br><span class="hljs-comment">     */</span><br>    <span class="hljs-keyword">public</span> &lt;T&gt; List&lt;T&gt; <span class="hljs-title function_">getCacheList</span><span class="hljs-params">(<span class="hljs-keyword">final</span> String key)</span><br>    &#123;<br>        <span class="hljs-keyword">return</span> redisTemplate.opsForList().range(key, <span class="hljs-number">0</span>, -<span class="hljs-number">1</span>);<br>    &#125;<br><br>    <span class="hljs-comment">/**</span><br><span class="hljs-comment">     * 缓存Set</span><br><span class="hljs-comment">     *</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> key 缓存键值</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> dataSet 缓存的数据</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@return</span> 缓存数据的对象</span><br><span class="hljs-comment">     */</span><br>    <span class="hljs-keyword">public</span> &lt;T&gt; BoundSetOperations&lt;String, T&gt; <span class="hljs-title function_">setCacheSet</span><span class="hljs-params">(<span class="hljs-keyword">final</span> String key, <span class="hljs-keyword">final</span> Set&lt;T&gt; dataSet)</span><br>    &#123;<br>        BoundSetOperations&lt;String, T&gt; setOperation = redisTemplate.boundSetOps(key);<br>        Iterator&lt;T&gt; it = dataSet.iterator();<br>        <span class="hljs-keyword">while</span> (it.hasNext())<br>        &#123;<br>            setOperation.add(it.next());<br>        &#125;<br>        <span class="hljs-keyword">return</span> setOperation;<br>    &#125;<br><br>    <span class="hljs-comment">/**</span><br><span class="hljs-comment">     * 获得缓存的set</span><br><span class="hljs-comment">     *</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> key</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@return</span></span><br><span class="hljs-comment">     */</span><br>    <span class="hljs-keyword">public</span> &lt;T&gt; Set&lt;T&gt; <span class="hljs-title function_">getCacheSet</span><span class="hljs-params">(<span class="hljs-keyword">final</span> String key)</span><br>    &#123;<br>        <span class="hljs-keyword">return</span> redisTemplate.opsForSet().members(key);<br>    &#125;<br><br>    <span class="hljs-comment">/**</span><br><span class="hljs-comment">     * 缓存Map</span><br><span class="hljs-comment">     *</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> key</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> dataMap</span><br><span class="hljs-comment">     */</span><br>    <span class="hljs-keyword">public</span> &lt;T&gt; <span class="hljs-keyword">void</span> <span class="hljs-title function_">setCacheMap</span><span class="hljs-params">(<span class="hljs-keyword">final</span> String key, <span class="hljs-keyword">final</span> Map&lt;String, T&gt; dataMap)</span><br>    &#123;<br>        <span class="hljs-keyword">if</span> (dataMap != <span class="hljs-literal">null</span>) &#123;<br>            redisTemplate.opsForHash().putAll(key, dataMap);<br>        &#125;<br>    &#125;<br><br>    <span class="hljs-comment">/**</span><br><span class="hljs-comment">     * 获得缓存的Map</span><br><span class="hljs-comment">     *</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> key</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@return</span></span><br><span class="hljs-comment">     */</span><br>    <span class="hljs-keyword">public</span> &lt;T&gt; Map&lt;String, T&gt; <span class="hljs-title function_">getCacheMap</span><span class="hljs-params">(<span class="hljs-keyword">final</span> String key)</span><br>    &#123;<br>        <span class="hljs-keyword">return</span> redisTemplate.opsForHash().entries(key);<br>    &#125;<br><br>    <span class="hljs-comment">/**</span><br><span class="hljs-comment">     * 往Hash中存入数据</span><br><span class="hljs-comment">     *</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> key Redis键</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> hKey Hash键</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> value 值</span><br><span class="hljs-comment">     */</span><br>    <span class="hljs-keyword">public</span> &lt;T&gt; <span class="hljs-keyword">void</span> <span class="hljs-title function_">setCacheMapValue</span><span class="hljs-params">(<span class="hljs-keyword">final</span> String key, <span class="hljs-keyword">final</span> String hKey, <span class="hljs-keyword">final</span> T value)</span><br>    &#123;<br>        redisTemplate.opsForHash().put(key, hKey, value);<br>    &#125;<br><br>    <span class="hljs-comment">/**</span><br><span class="hljs-comment">     * 获取Hash中的数据</span><br><span class="hljs-comment">     *</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> key Redis键</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> hKey Hash键</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@return</span> Hash中的对象</span><br><span class="hljs-comment">     */</span><br>    <span class="hljs-keyword">public</span> &lt;T&gt; T <span class="hljs-title function_">getCacheMapValue</span><span class="hljs-params">(<span class="hljs-keyword">final</span> String key, <span class="hljs-keyword">final</span> String hKey)</span><br>    &#123;<br>        HashOperations&lt;String, String, T&gt; opsForHash = redisTemplate.opsForHash();<br>        <span class="hljs-keyword">return</span> opsForHash.get(key, hKey);<br>    &#125;<br><br>    <span class="hljs-comment">/**</span><br><span class="hljs-comment">     * 删除Hash中的数据</span><br><span class="hljs-comment">     * </span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> key</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> hkey</span><br><span class="hljs-comment">     */</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">delCacheMapValue</span><span class="hljs-params">(<span class="hljs-keyword">final</span> String key, <span class="hljs-keyword">final</span> String hkey)</span><br>    &#123;<br>        <span class="hljs-type">HashOperations</span> <span class="hljs-variable">hashOperations</span> <span class="hljs-operator">=</span> redisTemplate.opsForHash();<br>        hashOperations.delete(key, hkey);<br>    &#125;<br><br>    <span class="hljs-comment">/**</span><br><span class="hljs-comment">     * 获取多个Hash中的数据</span><br><span class="hljs-comment">     *</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> key Redis键</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> hKeys Hash键集合</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@return</span> Hash对象集合</span><br><span class="hljs-comment">     */</span><br>    <span class="hljs-keyword">public</span> &lt;T&gt; List&lt;T&gt; <span class="hljs-title function_">getMultiCacheMapValue</span><span class="hljs-params">(<span class="hljs-keyword">final</span> String key, <span class="hljs-keyword">final</span> Collection&lt;Object&gt; hKeys)</span><br>    &#123;<br>        <span class="hljs-keyword">return</span> redisTemplate.opsForHash().multiGet(key, hKeys);<br>    &#125;<br><br>    <span class="hljs-comment">/**</span><br><span class="hljs-comment">     * 获得缓存的基本对象列表</span><br><span class="hljs-comment">     *</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> pattern 字符串前缀</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@return</span> 对象列表</span><br><span class="hljs-comment">     */</span><br>    <span class="hljs-keyword">public</span> Collection&lt;String&gt; <span class="hljs-title function_">keys</span><span class="hljs-params">(<span class="hljs-keyword">final</span> String pattern)</span><br>    &#123;<br>        <span class="hljs-keyword">return</span> redisTemplate.keys(pattern);<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><code class="hljs java"><br><span class="hljs-keyword">import</span> javax.servlet.http.HttpServletResponse;<br><span class="hljs-keyword">import</span> java.io.IOException;<br><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">WebUtils</span><br>&#123;<br>    <span class="hljs-comment">/**</span><br><span class="hljs-comment">     * 将字符串渲染到客户端</span><br><span class="hljs-comment">     * </span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> response 渲染对象</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> string 待渲染的字符串</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@return</span> null</span><br><span class="hljs-comment">     */</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> String <span class="hljs-title function_">renderString</span><span class="hljs-params">(HttpServletResponse response, String string)</span> &#123;<br>        <span class="hljs-keyword">try</span><br>        &#123;<br>            response.setStatus(<span class="hljs-number">200</span>);<br>            response.setContentType(<span class="hljs-string">&quot;application/json&quot;</span>);<br>            response.setCharacterEncoding(<span class="hljs-string">&quot;utf-8&quot;</span>);<br>            response.getWriter().print(string);<br>        &#125;<br>        <span class="hljs-keyword">catch</span> (IOException e)<br>        &#123;<br>            e.printStackTrace();<br>        &#125;<br>        <span class="hljs-keyword">return</span> <span class="hljs-literal">null</span>;<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<h2 id="JWT工具类"><a href="#JWT工具类" class="headerlink" title="JWT工具类"></a>JWT工具类</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br></pre></td><td class="code"><pre><code class="hljs java"><br><span class="hljs-keyword">import</span> io.jsonwebtoken.Claims;<br><span class="hljs-keyword">import</span> io.jsonwebtoken.JwtBuilder;<br><span class="hljs-keyword">import</span> io.jsonwebtoken.Jwts;<br><span class="hljs-keyword">import</span> io.jsonwebtoken.SignatureAlgorithm;<br><br><span class="hljs-keyword">import</span> javax.crypto.SecretKey;<br><span class="hljs-keyword">import</span> javax.crypto.spec.SecretKeySpec;<br><span class="hljs-keyword">import</span> java.util.Base64;<br><span class="hljs-keyword">import</span> java.util.Date;<br><span class="hljs-keyword">import</span> java.util.UUID;<br><br><span class="hljs-comment">/**</span><br><span class="hljs-comment"> * JWT工具类</span><br><span class="hljs-comment"> */</span><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">JwtUtil</span> &#123;<br><br>    <span class="hljs-comment">//有效期为</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">final</span> <span class="hljs-type">Long</span> <span class="hljs-variable">JWT_TTL</span> <span class="hljs-operator">=</span> <span class="hljs-number">60</span> * <span class="hljs-number">60</span> *<span class="hljs-number">1000L</span>;<span class="hljs-comment">// 60 * 60 *1000  一个小时</span><br>    <span class="hljs-comment">//设置秘钥明文</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">final</span> <span class="hljs-type">String</span> <span class="hljs-variable">JWT_KEY</span> <span class="hljs-operator">=</span> <span class="hljs-string">&quot;sangeng&quot;</span>;<br><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> String <span class="hljs-title function_">getUUID</span><span class="hljs-params">()</span>&#123;<br>        <span class="hljs-type">String</span> <span class="hljs-variable">token</span> <span class="hljs-operator">=</span> UUID.randomUUID().toString().replaceAll(<span class="hljs-string">&quot;-&quot;</span>, <span class="hljs-string">&quot;&quot;</span>);<br>        <span class="hljs-keyword">return</span> token;<br>    &#125;<br>    <br>    <span class="hljs-comment">/**</span><br><span class="hljs-comment">     * 生成jtw</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> subject token中要存放的数据（json格式）</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@return</span></span><br><span class="hljs-comment">     */</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> String <span class="hljs-title function_">createJWT</span><span class="hljs-params">(String subject)</span> &#123;<br>        <span class="hljs-type">JwtBuilder</span> <span class="hljs-variable">builder</span> <span class="hljs-operator">=</span> getJwtBuilder(subject, <span class="hljs-literal">null</span>, getUUID());<span class="hljs-comment">// 设置过期时间</span><br>        <span class="hljs-keyword">return</span> builder.compact();<br>    &#125;<br><br>    <span class="hljs-comment">/**</span><br><span class="hljs-comment">     * 生成jtw</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> subject token中要存放的数据（json格式）</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> ttlMillis token超时时间</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@return</span></span><br><span class="hljs-comment">     */</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> String <span class="hljs-title function_">createJWT</span><span class="hljs-params">(String subject, Long ttlMillis)</span> &#123;<br>        <span class="hljs-type">JwtBuilder</span> <span class="hljs-variable">builder</span> <span class="hljs-operator">=</span> getJwtBuilder(subject, ttlMillis, getUUID());<span class="hljs-comment">// 设置过期时间</span><br>        <span class="hljs-keyword">return</span> builder.compact();<br>    &#125;<br><br>    <span class="hljs-keyword">private</span> <span class="hljs-keyword">static</span> JwtBuilder <span class="hljs-title function_">getJwtBuilder</span><span class="hljs-params">(String subject, Long ttlMillis, String uuid)</span> &#123;<br>        <span class="hljs-type">SignatureAlgorithm</span> <span class="hljs-variable">signatureAlgorithm</span> <span class="hljs-operator">=</span> SignatureAlgorithm.HS256;<br>        <span class="hljs-type">SecretKey</span> <span class="hljs-variable">secretKey</span> <span class="hljs-operator">=</span> generalKey();<br>        <span class="hljs-type">long</span> <span class="hljs-variable">nowMillis</span> <span class="hljs-operator">=</span> System.currentTimeMillis();<br>        <span class="hljs-type">Date</span> <span class="hljs-variable">now</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">Date</span>(nowMillis);<br>        <span class="hljs-keyword">if</span>(ttlMillis==<span class="hljs-literal">null</span>)&#123;<br>            ttlMillis=JwtUtil.JWT_TTL;<br>        &#125;<br>        <span class="hljs-type">long</span> <span class="hljs-variable">expMillis</span> <span class="hljs-operator">=</span> nowMillis + ttlMillis;<br>        <span class="hljs-type">Date</span> <span class="hljs-variable">expDate</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">Date</span>(expMillis);<br>        <span class="hljs-keyword">return</span> Jwts.builder()<br>                .setId(uuid)              <span class="hljs-comment">//唯一的ID</span><br>                .setSubject(subject)   <span class="hljs-comment">// 主题  可以是JSON数据</span><br>                .setIssuer(<span class="hljs-string">&quot;sg&quot;</span>)     <span class="hljs-comment">// 签发者</span><br>                .setIssuedAt(now)      <span class="hljs-comment">// 签发时间</span><br>                .signWith(signatureAlgorithm, secretKey) <span class="hljs-comment">//使用HS256对称加密算法签名, 第二个参数为秘钥</span><br>                .setExpiration(expDate);<br>    &#125;<br><br>    <span class="hljs-comment">/**</span><br><span class="hljs-comment">     * 创建token</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> id</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> subject</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> ttlMillis</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@return</span></span><br><span class="hljs-comment">     */</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> String <span class="hljs-title function_">createJWT</span><span class="hljs-params">(String id, String subject, Long ttlMillis)</span> &#123;<br>        <span class="hljs-type">JwtBuilder</span> <span class="hljs-variable">builder</span> <span class="hljs-operator">=</span> getJwtBuilder(subject, ttlMillis, id);<span class="hljs-comment">// 设置过期时间</span><br>        <span class="hljs-keyword">return</span> builder.compact();<br>    &#125;<br><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">main</span><span class="hljs-params">(String[] args)</span> <span class="hljs-keyword">throws</span> Exception &#123;<br>        <span class="hljs-type">String</span> <span class="hljs-variable">token</span> <span class="hljs-operator">=</span> <span class="hljs-string">&quot;eyJhbGciOiJIUzI1NiJ9.eyJqdGkiOiJjYWM2ZDVhZi1mNjVlLTQ0MDAtYjcxMi0zYWEwOGIyOTIwYjQiLCJzdWIiOiJzZyIsImlzcyI6InNnIiwiaWF0IjoxNjM4MTA2NzEyLCJleHAiOjE2MzgxMTAzMTJ9.JVsSbkP94wuczb4QryQbAke3ysBDIL5ou8fWsbt_ebg&quot;</span>;<br>        <span class="hljs-type">Claims</span> <span class="hljs-variable">claims</span> <span class="hljs-operator">=</span> parseJWT(token);<br>        System.out.println(claims);<br>    &#125;<br><br>    <span class="hljs-comment">/**</span><br><span class="hljs-comment">     * 生成加密后的秘钥 secretKey</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@return</span></span><br><span class="hljs-comment">     */</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> SecretKey <span class="hljs-title function_">generalKey</span><span class="hljs-params">()</span> &#123;<br>        <span class="hljs-type">byte</span>[] encodedKey = Base64.getDecoder().decode(JwtUtil.JWT_KEY);<br>        <span class="hljs-type">SecretKey</span> <span class="hljs-variable">key</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">SecretKeySpec</span>(encodedKey, <span class="hljs-number">0</span>, encodedKey.length, <span class="hljs-string">&quot;AES&quot;</span>);<br>        <span class="hljs-keyword">return</span> key;<br>    &#125;<br>    <br>    <span class="hljs-comment">/**</span><br><span class="hljs-comment">     * 解析</span><br><span class="hljs-comment">     *</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> jwt</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@return</span></span><br><span class="hljs-comment">     * <span class="hljs-doctag">@throws</span> Exception</span><br><span class="hljs-comment">     */</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> Claims <span class="hljs-title function_">parseJWT</span><span class="hljs-params">(String jwt)</span> <span class="hljs-keyword">throws</span> Exception &#123;<br>        <span class="hljs-type">SecretKey</span> <span class="hljs-variable">secretKey</span> <span class="hljs-operator">=</span> generalKey();<br>        <span class="hljs-keyword">return</span> Jwts.parser()<br>                .setSigningKey(secretKey)<br>                .parseClaimsJws(jwt)<br>                .getBody();<br>    &#125;<br><br>&#125;<br></code></pre></td></tr></table></figure>

<h2 id="密码加密存储"><a href="#密码加密存储" class="headerlink" title="密码加密存储"></a>密码加密存储</h2><p>​	我们一般使用SpringSecurity为我们提供的BCryptPasswordEncoder。</p>
<p>​	我们只需要使用把BCryptPasswordEncoder对象注入Spring容器中，SpringSecurity就会使用该PasswordEncoder来进行密码校验。</p>
<p>​	我们可以定义一个SpringSecurity的配置类，SpringSecurity要求这个配置类要继承WebSecurityConfigurerAdapter。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-comment">/**</span><br><span class="hljs-comment"> * <span class="hljs-doctag">@Author</span> 三更  B站： https://space.bilibili.com/663528522</span><br><span class="hljs-comment"> */</span><br><span class="hljs-meta">@Configuration</span><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">SecurityConfig</span> <span class="hljs-keyword">extends</span> <span class="hljs-title class_">WebSecurityConfigurerAdapter</span> &#123;<br><br><br>    <span class="hljs-meta">@Bean</span><br>    <span class="hljs-keyword">public</span> PasswordEncoder <span class="hljs-title function_">passwordEncoder</span><span class="hljs-params">()</span>&#123;<br>        <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">BCryptPasswordEncoder</span>();<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<h2 id="加密解密工具类"><a href="#加密解密工具类" class="headerlink" title="加密解密工具类"></a>加密解密工具类</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">import</span> javax.crypto.Cipher;<br><span class="hljs-keyword">import</span> javax.crypto.KeyGenerator;<br><span class="hljs-keyword">import</span> javax.crypto.SecretKey;<br><span class="hljs-keyword">import</span> javax.crypto.spec.SecretKeySpec;<br><span class="hljs-keyword">import</span> java.security.SecureRandom;<br><br><span class="hljs-comment">// 加密、解密工具类</span><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">CryptUtil</span> &#123;<br>    <span class="hljs-keyword">private</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">final</span> <span class="hljs-type">String</span> <span class="hljs-variable">AES</span> <span class="hljs-operator">=</span> <span class="hljs-string">&quot;AES&quot;</span>;<br>    <span class="hljs-keyword">private</span> <span class="hljs-keyword">static</span> <span class="hljs-type">int</span> <span class="hljs-variable">keysizeAES</span> <span class="hljs-operator">=</span> <span class="hljs-number">128</span>;<br>    <span class="hljs-keyword">private</span> <span class="hljs-keyword">static</span> <span class="hljs-type">String</span> <span class="hljs-variable">charset</span> <span class="hljs-operator">=</span> <span class="hljs-string">&quot;UTF-8&quot;</span>;<br><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> String <span class="hljs-title function_">parseByte2HexStr</span><span class="hljs-params">(<span class="hljs-keyword">final</span> <span class="hljs-type">byte</span>[] b)</span> &#123;<br>        <span class="hljs-keyword">final</span> <span class="hljs-type">StringBuilder</span> <span class="hljs-variable">stringBuffer</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">StringBuilder</span>();<br>        <span class="hljs-keyword">for</span> (<span class="hljs-type">byte</span> value : b) &#123;<br>            <span class="hljs-type">String</span> <span class="hljs-variable">hex</span> <span class="hljs-operator">=</span> Integer.toHexString(value &amp; <span class="hljs-number">0xFF</span>);<br>            <span class="hljs-keyword">if</span> (hex.length() == <span class="hljs-number">1</span>) &#123;<br>                hex = <span class="hljs-string">&#x27;0&#x27;</span> + hex;<br>            &#125;<br>            stringBuffer.append(hex.toUpperCase());<br>        &#125;<br>        <span class="hljs-keyword">return</span> stringBuffer.toString();<br>    &#125;<br><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-type">byte</span>[] parseHexStr2Byte(<span class="hljs-keyword">final</span> String hexStr) &#123;<br>        <span class="hljs-keyword">if</span> (hexStr.length() &lt; <span class="hljs-number">1</span>) &#123;<br>            <span class="hljs-keyword">return</span> <span class="hljs-literal">null</span>;<br>        &#125;<br>        <span class="hljs-keyword">final</span> <span class="hljs-type">byte</span>[] result = <span class="hljs-keyword">new</span> <span class="hljs-title class_">byte</span>[hexStr.length() / <span class="hljs-number">2</span>];<br>        <span class="hljs-keyword">for</span> (<span class="hljs-type">int</span> <span class="hljs-variable">i</span> <span class="hljs-operator">=</span> <span class="hljs-number">0</span>; i &lt; hexStr.length() / <span class="hljs-number">2</span>; i++) &#123;<br>            <span class="hljs-type">int</span> <span class="hljs-variable">high</span> <span class="hljs-operator">=</span> Integer.parseInt(hexStr.substring(i * <span class="hljs-number">2</span>, i * <span class="hljs-number">2</span> + <span class="hljs-number">1</span>), <span class="hljs-number">16</span>);<br>            <span class="hljs-type">int</span> <span class="hljs-variable">low</span> <span class="hljs-operator">=</span> Integer.parseInt(hexStr.substring(i * <span class="hljs-number">2</span> + <span class="hljs-number">1</span>, i * <span class="hljs-number">2</span> + <span class="hljs-number">2</span>), <span class="hljs-number">16</span>);<br>            result[i] = (<span class="hljs-type">byte</span>) (high * <span class="hljs-number">16</span> + low);<br>        &#125;<br>        <span class="hljs-keyword">return</span> result;<br>    &#125;<br><br>    <span class="hljs-keyword">private</span> <span class="hljs-keyword">static</span> String <span class="hljs-title function_">keyGeneratorES</span><span class="hljs-params">(<span class="hljs-keyword">final</span> String res, <span class="hljs-keyword">final</span> String algorithm, <span class="hljs-keyword">final</span> String key, <span class="hljs-keyword">final</span> Integer keysize, <span class="hljs-keyword">final</span> <span class="hljs-type">boolean</span> bEncode)</span> &#123;<br>        <span class="hljs-keyword">try</span> &#123;<br>            <span class="hljs-keyword">final</span> <span class="hljs-type">KeyGenerator</span> <span class="hljs-variable">g</span> <span class="hljs-operator">=</span> KeyGenerator.getInstance(algorithm);<br>            <span class="hljs-keyword">if</span> (keysize == <span class="hljs-number">0</span>) &#123;<br>                <span class="hljs-type">byte</span>[] keyBytes = charset == <span class="hljs-literal">null</span> ? key.getBytes() : key.getBytes(charset);<br>                g.init(<span class="hljs-keyword">new</span> <span class="hljs-title class_">SecureRandom</span>(keyBytes));<br>            &#125; <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (key == <span class="hljs-literal">null</span>) &#123;<br>                g.init(keysize);<br>            &#125; <span class="hljs-keyword">else</span> &#123;<br>                <span class="hljs-type">byte</span>[] keyBytes = charset == <span class="hljs-literal">null</span> ? key.getBytes() : key.getBytes(charset);<br>                <span class="hljs-type">SecureRandom</span> <span class="hljs-variable">random</span> <span class="hljs-operator">=</span> SecureRandom.getInstance(<span class="hljs-string">&quot;SHA1PRNG&quot;</span>);<br>                random.setSeed(keyBytes);<br>                g.init(keysize, random);<br>            &#125;<br>            <span class="hljs-keyword">final</span> <span class="hljs-type">SecretKey</span> <span class="hljs-variable">secretKey</span> <span class="hljs-operator">=</span> g.generateKey();<br>            <span class="hljs-keyword">final</span> <span class="hljs-type">SecretKeySpec</span> <span class="hljs-variable">keySpec</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">SecretKeySpec</span>(secretKey.getEncoded(), algorithm);<br>            <span class="hljs-keyword">final</span> <span class="hljs-type">Cipher</span> <span class="hljs-variable">cipher</span> <span class="hljs-operator">=</span> Cipher.getInstance(algorithm);<br>            <span class="hljs-keyword">if</span> (bEncode) &#123;<br>                cipher.init(Cipher.ENCRYPT_MODE, keySpec);<br>                <span class="hljs-keyword">final</span> <span class="hljs-type">byte</span>[] result = charset == <span class="hljs-literal">null</span> ? res.getBytes() : res.getBytes(charset);<br>                <span class="hljs-keyword">return</span> parseByte2HexStr(cipher.doFinal(result));<br>            &#125; <span class="hljs-keyword">else</span> &#123;<br>                cipher.init(Cipher.DECRYPT_MODE, keySpec);<br>                <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">String</span>(cipher.doFinal(parseHexStr2Byte(res)));<br>            &#125;<br>        &#125; <span class="hljs-keyword">catch</span> (Exception e) &#123;<br>            e.printStackTrace();<br>        &#125;<br>        <span class="hljs-keyword">return</span> <span class="hljs-literal">null</span>;<br>    &#125;<br><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> String <span class="hljs-title function_">AESencode</span><span class="hljs-params">(<span class="hljs-keyword">final</span> String res)</span> &#123;<br>        <span class="hljs-keyword">return</span> keyGeneratorES(res, AES, <span class="hljs-string">&quot;aAll*-%&quot;</span>, keysizeAES, <span class="hljs-literal">true</span>);<br>    &#125;<br><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> String <span class="hljs-title function_">AESdecode</span><span class="hljs-params">(<span class="hljs-keyword">final</span> String res)</span> &#123;<br>        <span class="hljs-keyword">return</span> keyGeneratorES(res, AES, <span class="hljs-string">&quot;aAll*-%&quot;</span>, keysizeAES, <span class="hljs-literal">false</span>);<br>    &#125;<br><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">main</span><span class="hljs-params">(String[] args)</span> &#123;<br>        System.out.println(<span class="hljs-string">&quot;加密后:&quot;</span> + AESencode(<span class="hljs-string">&quot;123456&quot;</span>));<br>        System.out.println(<span class="hljs-string">&quot;解密后:&quot;</span> + AESdecode(<span class="hljs-string">&quot;555B695B57E024EEA169EAE275B9D93B&quot;</span>));<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<h2 id="常用pom依赖"><a href="#常用pom依赖" class="headerlink" title="常用pom依赖"></a>常用pom依赖</h2><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><code class="hljs xml"><span class="hljs-comment">&lt;!--Swagger3.x--&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">dependency</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">groupId</span>&gt;</span>io.springfox<span class="hljs-tag">&lt;/<span class="hljs-name">groupId</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">artifactId</span>&gt;</span>springfox-boot-starter<span class="hljs-tag">&lt;/<span class="hljs-name">artifactId</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">version</span>&gt;</span>3.0.0<span class="hljs-tag">&lt;/<span class="hljs-name">version</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">dependency</span>&gt;</span><br><span class="hljs-comment">&lt;!--MyBatis-plus自动生成--&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">dependency</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">groupId</span>&gt;</span>com.baomidou<span class="hljs-tag">&lt;/<span class="hljs-name">groupId</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">artifactId</span>&gt;</span>mybatis-plus-generator<span class="hljs-tag">&lt;/<span class="hljs-name">artifactId</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">version</span>&gt;</span>3.4.1<span class="hljs-tag">&lt;/<span class="hljs-name">version</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">dependency</span>&gt;</span><br><span class="hljs-comment">&lt;!--velocity--&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">dependency</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">groupId</span>&gt;</span>org.apache.velocity<span class="hljs-tag">&lt;/<span class="hljs-name">groupId</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">artifactId</span>&gt;</span>velocity-engine-core<span class="hljs-tag">&lt;/<span class="hljs-name">artifactId</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">version</span>&gt;</span>2.0<span class="hljs-tag">&lt;/<span class="hljs-name">version</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">dependency</span>&gt;</span><br><span class="hljs-comment">&lt;!--代码自动生成依赖--&gt;</span><br><span class="hljs-comment">&lt;!--Json--&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">dependency</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">groupId</span>&gt;</span>com.vaadin.external.google<span class="hljs-tag">&lt;/<span class="hljs-name">groupId</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">artifactId</span>&gt;</span>android-json<span class="hljs-tag">&lt;/<span class="hljs-name">artifactId</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">version</span>&gt;</span>0.0.20131108.vaadin1<span class="hljs-tag">&lt;/<span class="hljs-name">version</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">scope</span>&gt;</span>compile<span class="hljs-tag">&lt;/<span class="hljs-name">scope</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">dependency</span>&gt;</span><br><span class="hljs-comment">&lt;!--Swagger--&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">dependency</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">groupId</span>&gt;</span>io.swagger<span class="hljs-tag">&lt;/<span class="hljs-name">groupId</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">artifactId</span>&gt;</span>swagger-annotations<span class="hljs-tag">&lt;/<span class="hljs-name">artifactId</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">version</span>&gt;</span>1.5.22<span class="hljs-tag">&lt;/<span class="hljs-name">version</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">dependency</span>&gt;</span><br></code></pre></td></tr></table></figure>

<h2 id="swagger配置"><a href="#swagger配置" class="headerlink" title="swagger配置"></a>swagger配置</h2><blockquote>
<p>访问<code>http://localhost:8090/swagger-ui/index.html</code>即可查看接口文档</p>
</blockquote>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><code class="hljs xml"><span class="hljs-comment">&lt;!--Swagger3.x--&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">dependency</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">groupId</span>&gt;</span>io.springfox<span class="hljs-tag">&lt;/<span class="hljs-name">groupId</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">artifactId</span>&gt;</span>springfox-boot-starter<span class="hljs-tag">&lt;/<span class="hljs-name">artifactId</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">version</span>&gt;</span>3.0.0<span class="hljs-tag">&lt;/<span class="hljs-name">version</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">dependency</span>&gt;</span><br></code></pre></td></tr></table></figure>

<figure class="highlight properties"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><code class="hljs properties"><span class="hljs-comment"># ==========自定义swagger配置==========</span><br><span class="hljs-attr">swagger.enable</span>=<span class="hljs-string">true</span><br><span class="hljs-attr">swagger.application-name</span>=<span class="hljs-string">$&#123;spring.application.name&#125;</span><br><span class="hljs-attr">swagger.application-version</span>=<span class="hljs-string">1.0</span><br><span class="hljs-attr">swagger.application-description</span>=<span class="hljs-string">1024shop api info</span><br><span class="hljs-comment">#swagger.application-description=1024shop电商平台管理后端接口文档</span><br><span class="hljs-comment"># 这里如果是中文，则会出现乱码，有以下两种解决方法</span><br><span class="hljs-comment"># 1. 修改文件编码</span><br><span class="hljs-comment"># 2. 使用yml格式</span><br></code></pre></td></tr></table></figure>

<p><img src="https://gitee.com/tongstyle/blog-img/raw/master/img/202303051728479.png" srcset="/img/loading.gif" lazyload alt="image-20230305172841417"></p>
<p><img src="https://gitee.com/tongstyle/blog-img/raw/master/img/202303051728336.png" srcset="/img/loading.gif" lazyload alt="image-20230305172859280"></p>
<blockquote>
<p>编码格式改为了<code>ISO-8859-1</code>，如果是配置yml文件，格式也需要更改</p>
</blockquote>
<p><img src="https://gitee.com/tongstyle/blog-img/raw/master/img/202303051729582.png" srcset="/img/loading.gif" lazyload alt="image-20230305172922513"></p>
<p><img src="https://gitee.com/tongstyle/blog-img/raw/master/img/202303051729342.png" srcset="/img/loading.gif" lazyload alt="@PropertySource读取配置文件"></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">import</span> io.swagger.annotations.ApiOperation;<br><span class="hljs-keyword">import</span> lombok.Data;<br><span class="hljs-keyword">import</span> org.springframework.boot.context.properties.ConfigurationProperties;<br><span class="hljs-keyword">import</span> org.springframework.context.annotation.Bean;<br><span class="hljs-keyword">import</span> org.springframework.stereotype.Component;<br><span class="hljs-keyword">import</span> springfox.documentation.builders.ApiInfoBuilder;<br><span class="hljs-keyword">import</span> springfox.documentation.builders.PathSelectors;<br><span class="hljs-keyword">import</span> springfox.documentation.builders.RequestHandlerSelectors;<br><span class="hljs-keyword">import</span> springfox.documentation.oas.annotations.EnableOpenApi;<br><span class="hljs-keyword">import</span> springfox.documentation.service.ApiInfo;<br><span class="hljs-keyword">import</span> springfox.documentation.service.Contact;<br><span class="hljs-keyword">import</span> springfox.documentation.spi.DocumentationType;<br><span class="hljs-keyword">import</span> springfox.documentation.spring.web.plugins.Docket;<br><br><span class="hljs-comment">/**</span><br><span class="hljs-comment"> * <span class="hljs-doctag">@Auther</span>: 温豪</span><br><span class="hljs-comment"> * <span class="hljs-doctag">@Date</span>: 2022/11/30/15:05</span><br><span class="hljs-comment"> * <span class="hljs-doctag">@Description</span>:</span><br><span class="hljs-comment"> */</span><br><span class="hljs-meta">@Component</span><br><span class="hljs-meta">@Data</span><br><span class="hljs-meta">@ConfigurationProperties(&quot;swagger&quot;)</span> <span class="hljs-comment">// 映射到properties配置，省略前缀</span><br><span class="hljs-meta">@EnableOpenApi</span> <span class="hljs-comment">// 开启规范</span><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">SwaggerConfiguration</span> &#123;<br>    <span class="hljs-comment">/**</span><br><span class="hljs-comment">     * 是否开启swagger，生产环境一般关闭，所以这里定义一个变量</span><br><span class="hljs-comment">     */</span><br>    <span class="hljs-keyword">private</span> Boolean enable;<br>    <span class="hljs-comment">/**</span><br><span class="hljs-comment">     * 项目应用名</span><br><span class="hljs-comment">     */</span><br>    <span class="hljs-keyword">private</span> String applicationName;<br>    <span class="hljs-comment">/**</span><br><span class="hljs-comment">     * 项目版本信息</span><br><span class="hljs-comment">     */</span><br>    <span class="hljs-keyword">private</span> String applicationVersion;<br>    <span class="hljs-comment">/**</span><br><span class="hljs-comment">     * 项目描述信息</span><br><span class="hljs-comment">     */</span><br>    <span class="hljs-keyword">private</span> String applicationDescription;<br>    <span class="hljs-meta">@Bean</span><br>    <span class="hljs-keyword">public</span> Docket <span class="hljs-title function_">docket</span><span class="hljs-params">()</span> &#123;<br>        <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">Docket</span>(DocumentationType.OAS_30)<br>                .pathMapping(<span class="hljs-string">&quot;/&quot;</span>)<br>                <span class="hljs-comment">// 定义是否开启swagger，false为关闭，可以通过变量控制，线上关闭</span><br>                .enable(enable)<br>                <span class="hljs-comment">//配置api文档元信息</span><br>                .apiInfo(apiInfo())<br>                <span class="hljs-comment">// 选择哪些接口作为swagger的doc发布</span><br>                .select()<br>                <span class="hljs-comment">//apis() 控制哪些接口暴露给swagger，</span><br>                <span class="hljs-comment">// RequestHandlerSelectors.any() 所有都暴露</span><br>                <span class="hljs-comment">// RequestHandlerSelectors.basePackage(&quot;net.xdclass.*&quot;)  指定包位置</span><br>                <span class="hljs-comment">// withMethodAnnotation(ApiOperation.class)标记有这个注解 ApiOperation</span><br>                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))<br>                .paths(PathSelectors.any())<br>                .build();<br>    &#125;<br>    <span class="hljs-keyword">private</span> ApiInfo <span class="hljs-title function_">apiInfo</span><span class="hljs-params">()</span> &#123;<br>        <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">ApiInfoBuilder</span>()<br>                .title(applicationName)<br>                .description(applicationDescription)<br>                .contact(<span class="hljs-keyword">new</span> <span class="hljs-title class_">Contact</span>(<span class="hljs-string">&quot;深海火锅店&quot;</span>, <span class="hljs-string">&quot;tongstyle.gitee.io&quot;</span>, <span class="hljs-string">&quot;938798576@aliyun.com&quot;</span>))<br>                .version(applicationVersion)<br>                .build();<br>    &#125;<br><br>&#125;<br></code></pre></td></tr></table></figure>

<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-comment">// 主要注解</span><br><span class="hljs-comment">// 1.@Api()</span><br><span class="hljs-comment">// 2.@ApiOperation()</span><br><span class="hljs-comment">// 3.@ApiResponse()</span><br><span class="hljs-comment">// 4.@ApiModel()</span><br><span class="hljs-comment">// 5.@ApiModelProperty()</span><br><br><span class="hljs-meta">@RestController</span><br><span class="hljs-meta">@RequestMapping(&quot;/user&quot;)</span><br><span class="hljs-meta">@Api(tags = &quot;用户模块&quot;, value = &quot;用户controller&quot;)</span><br><br><span class="hljs-comment">// --------------------------------------------------</span><br><span class="hljs-meta">@ApiOperation(&quot;用户登录&quot;)</span><br><span class="hljs-meta">@ApiResponses(&#123;</span><br><span class="hljs-meta">  @ApiResponse(responseCode = &quot;404&quot;,description = &quot;找不到该页面&quot;),</span><br><span class="hljs-meta">  @ApiResponse(responseCode = &quot;403&quot;,description = &quot;权限不够&quot;)</span><br><span class="hljs-meta">&#125;)</span><br><span class="hljs-meta">@ApiImplicitParams(&#123;</span><br><span class="hljs-meta">			@ApiImplicitParam(name = &quot;mobile&quot;, value = &quot;手机号码&quot;, dataType = &quot;string&quot;, paramType = &quot;query&quot;, example = &quot;13802780104&quot;, required = true),</span><br><span class="hljs-meta">			@ApiImplicitParam(name = &quot;user_name&quot;, value = &quot;登录账号&quot;, dataType = &quot;string&quot;, paramType = &quot;query&quot;, example = &quot;lihailin9073&quot;, required = true), &#125;)</span><br><span class="hljs-meta">@PostMapping(&quot;/login&quot;)</span><br><span class="hljs-keyword">public</span> Object <span class="hljs-title function_">login</span><span class="hljs-params">(<span class="hljs-meta">@RequestBody</span> User user)</span> &#123;<br>  <span class="hljs-keyword">return</span> userService.login(user);<br>&#125;<br><span class="hljs-comment">// --------------------------------------------------</span><br><span class="hljs-comment">// 这里千万要给get、set方法或者Data注解</span><br><span class="hljs-meta">@Data</span><br><span class="hljs-meta">@ApiModel(value = &quot;User对象&quot;, description = &quot;用户模型&quot;)</span><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">User</span> <span class="hljs-keyword">implements</span> <span class="hljs-title class_">Serializable</span> &#123;<br><br>    <span class="hljs-keyword">private</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">final</span> <span class="hljs-type">long</span> <span class="hljs-variable">serialVersionUID</span> <span class="hljs-operator">=</span> <span class="hljs-number">1L</span>;<br><br>    <span class="hljs-meta">@TableId(value = &quot;id&quot;, type = IdType.AUTO)</span><br>    <span class="hljs-meta">@ApiModelProperty(value = &quot;主键&quot;)</span><br>    <span class="hljs-keyword">private</span> Integer id;<br><br>    <span class="hljs-meta">@TableField(&quot;username&quot;)</span><br>    <span class="hljs-meta">@ApiModelProperty(value = &quot;用户名&quot;, required = true, example = &quot;admin&quot;)</span><br>    <span class="hljs-keyword">private</span> String username;<br><br>    <span class="hljs-meta">@TableField(&quot;password&quot;)</span><br>    <span class="hljs-meta">@ApiModelProperty(value = &quot;密码&quot;, required = true, example = &quot;123456&quot;)</span><br>    <span class="hljs-keyword">private</span> String password;<br>&#125;<br></code></pre></td></tr></table></figure>

<blockquote>
<p>不加<code>@RequestBody</code>的情景，可以直接在<code>Parameters</code>获取到</p>
</blockquote>
<p><img src="https://gitee.com/tongstyle/blog-img/raw/master/img/202303051712583.png" srcset="/img/loading.gif" lazyload alt="image-20230305171247415"></p>
<blockquote>
<p>加了<code>@RequestBody</code>的情景，显示<code>No parameters</code>，但<code>Schema</code>处可看到参数详情</p>
</blockquote>
<p><img src="https://gitee.com/tongstyle/blog-img/raw/master/img/202303051714339.png" srcset="/img/loading.gif" lazyload alt="image-20230305171415219"></p>
<p><img src="https://gitee.com/tongstyle/blog-img/raw/master/img/202303051714998.png" srcset="/img/loading.gif" lazyload alt="image-20230305171448855"></p>
<h2 id="MyBatis-plus配置"><a href="#MyBatis-plus配置" class="headerlink" title="MyBatis-plus配置"></a>MyBatis-plus配置</h2><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br></pre></td><td class="code"><pre><code class="hljs xml"><span class="hljs-comment">&lt;!-- fastjson --&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">dependency</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">groupId</span>&gt;</span>com.alibaba<span class="hljs-tag">&lt;/<span class="hljs-name">groupId</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">artifactId</span>&gt;</span>fastjson<span class="hljs-tag">&lt;/<span class="hljs-name">artifactId</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">version</span>&gt;</span>1.2.72<span class="hljs-tag">&lt;/<span class="hljs-name">version</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">dependency</span>&gt;</span><br><span class="hljs-comment">&lt;!--Swagger3.x--&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">dependency</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">groupId</span>&gt;</span>io.springfox<span class="hljs-tag">&lt;/<span class="hljs-name">groupId</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">artifactId</span>&gt;</span>springfox-boot-starter<span class="hljs-tag">&lt;/<span class="hljs-name">artifactId</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">version</span>&gt;</span>3.0.0<span class="hljs-tag">&lt;/<span class="hljs-name">version</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">dependency</span>&gt;</span><br><span class="hljs-comment">&lt;!--新版 mybatis-plus 自动生成器--&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">dependency</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">groupId</span>&gt;</span>com.baomidou<span class="hljs-tag">&lt;/<span class="hljs-name">groupId</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">artifactId</span>&gt;</span>mybatis-plus-generator<span class="hljs-tag">&lt;/<span class="hljs-name">artifactId</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">version</span>&gt;</span>3.5.2<span class="hljs-tag">&lt;/<span class="hljs-name">version</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">dependency</span>&gt;</span><br><span class="hljs-comment">&lt;!-- freemarker --&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">dependency</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">groupId</span>&gt;</span>org.freemarker<span class="hljs-tag">&lt;/<span class="hljs-name">groupId</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">artifactId</span>&gt;</span>freemarker<span class="hljs-tag">&lt;/<span class="hljs-name">artifactId</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">version</span>&gt;</span>2.3.31<span class="hljs-tag">&lt;/<span class="hljs-name">version</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">dependency</span>&gt;</span><br><span class="hljs-comment">&lt;!-- web --&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">dependency</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">groupId</span>&gt;</span>org.springframework.boot<span class="hljs-tag">&lt;/<span class="hljs-name">groupId</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">artifactId</span>&gt;</span>spring-boot-starter-web<span class="hljs-tag">&lt;/<span class="hljs-name">artifactId</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">dependency</span>&gt;</span><br><span class="hljs-comment">&lt;!-- mybatis-plus --&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">dependency</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">groupId</span>&gt;</span>com.baomidou<span class="hljs-tag">&lt;/<span class="hljs-name">groupId</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">artifactId</span>&gt;</span>mybatis-plus-boot-starter<span class="hljs-tag">&lt;/<span class="hljs-name">artifactId</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">version</span>&gt;</span>3.4.2<span class="hljs-tag">&lt;/<span class="hljs-name">version</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">dependency</span>&gt;</span><br><span class="hljs-comment">&lt;!-- mysql --&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">dependency</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">groupId</span>&gt;</span>mysql<span class="hljs-tag">&lt;/<span class="hljs-name">groupId</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">artifactId</span>&gt;</span>mysql-connector-java<span class="hljs-tag">&lt;/<span class="hljs-name">artifactId</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">scope</span>&gt;</span>runtime<span class="hljs-tag">&lt;/<span class="hljs-name">scope</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">dependency</span>&gt;</span><br><span class="hljs-comment">&lt;!-- lombok --&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">dependency</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">groupId</span>&gt;</span>org.projectlombok<span class="hljs-tag">&lt;/<span class="hljs-name">groupId</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">artifactId</span>&gt;</span>lombok<span class="hljs-tag">&lt;/<span class="hljs-name">artifactId</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">optional</span>&gt;</span>true<span class="hljs-tag">&lt;/<span class="hljs-name">optional</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">dependency</span>&gt;</span><br><span class="hljs-comment">&lt;!-- junit --&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">dependency</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">groupId</span>&gt;</span>org.springframework.boot<span class="hljs-tag">&lt;/<span class="hljs-name">groupId</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">artifactId</span>&gt;</span>spring-boot-starter-test<span class="hljs-tag">&lt;/<span class="hljs-name">artifactId</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">scope</span>&gt;</span>test<span class="hljs-tag">&lt;/<span class="hljs-name">scope</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">exclusions</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">exclusion</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">groupId</span>&gt;</span>org.junit.vintage<span class="hljs-tag">&lt;/<span class="hljs-name">groupId</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">artifactId</span>&gt;</span>junit-vintage-engine<span class="hljs-tag">&lt;/<span class="hljs-name">artifactId</span>&gt;</span><br>        <span class="hljs-tag">&lt;/<span class="hljs-name">exclusion</span>&gt;</span><br>    <span class="hljs-tag">&lt;/<span class="hljs-name">exclusions</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">dependency</span>&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">dependency</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">groupId</span>&gt;</span>junit<span class="hljs-tag">&lt;/<span class="hljs-name">groupId</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">artifactId</span>&gt;</span>junit<span class="hljs-tag">&lt;/<span class="hljs-name">artifactId</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">scope</span>&gt;</span>test<span class="hljs-tag">&lt;/<span class="hljs-name">scope</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">dependency</span>&gt;</span><br></code></pre></td></tr></table></figure>
<h3 id="自动生成"><a href="#自动生成" class="headerlink" title="自动生成"></a>自动生成</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">import</span> java.util.Collections;<br><span class="hljs-keyword">import</span> com.baomidou.mybatisplus.annotation.FieldFill;<br><span class="hljs-keyword">import</span> com.baomidou.mybatisplus.annotation.IdType;<br><span class="hljs-keyword">import</span> com.baomidou.mybatisplus.generator.FastAutoGenerator;<br><span class="hljs-keyword">import</span> com.baomidou.mybatisplus.generator.config.OutputFile;<br><span class="hljs-keyword">import</span> com.baomidou.mybatisplus.generator.config.rules.DateType;<br><span class="hljs-keyword">import</span> com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;<br><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">GeneratorTest</span> &#123;<br>	<span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">main</span><span class="hljs-params">(String[] args)</span> &#123;<br>		<span class="hljs-comment">// 配置相关的数据库连接</span><br>		<span class="hljs-comment">// TODO TODO</span><br>		FastAutoGenerator.create(<span class="hljs-string">&quot;jdbc:mysql://localhost:3306/test?serverTimezone=Asia/Shanghai&quot;</span>, <span class="hljs-string">&quot;root&quot;</span>, <span class="hljs-string">&quot;123456&quot;</span>)<br>				.globalConfig(builder -&gt; &#123;<br>					builder.author(<span class="hljs-string">&quot;深海火锅店&quot;</span>) <span class="hljs-comment">// 设置作者</span><br>							.enableSwagger() <span class="hljs-comment">// 开启 swagger 模式</span><br>							.fileOverride() <span class="hljs-comment">// 覆盖已生成文件</span><br>							<span class="hljs-comment">// TODO TODO</span><br>							.outputDir(<span class="hljs-string">&quot;src\\main\\java\\&quot;</span>) <span class="hljs-comment">// 指定输出目录</span><br>							.dateType(DateType.ONLY_DATE);<br>				&#125;).packageConfig(builder -&gt; &#123;<br>					<span class="hljs-comment">// TODO TODO</span><br>					builder.parent(<span class="hljs-string">&quot;com.example.demo&quot;</span>) <span class="hljs-comment">// 设置父包名</span><br><span class="hljs-comment">//                            .moduleName(&quot;&quot;) // 设置父包模块名</span><br>							.entity(<span class="hljs-string">&quot;domain&quot;</span>).controller(<span class="hljs-string">&quot;controller&quot;</span>).mapper(<span class="hljs-string">&quot;mapper&quot;</span>).service(<span class="hljs-string">&quot;service&quot;</span>)<br>							.serviceImpl(<span class="hljs-string">&quot;service.impl&quot;</span>)<br>							.pathInfo(Collections.singletonMap(OutputFile.xml, <span class="hljs-string">&quot;src\\main\\resources\\mapper\\&quot;</span>)); <span class="hljs-comment">// 设置mapperXml生成路径</span><br>				&#125;).strategyConfig(builder -&gt; &#123;<br>					<span class="hljs-comment">// TODO TODO</span><br>					builder.addInclude(<span class="hljs-string">&quot;user&quot;</span>)<span class="hljs-comment">// 设置需要生成的表名</span><br>					<span class="hljs-comment">// TODO TODO</span><br><span class="hljs-comment">//                            .addTablePrefix(&quot;tb_&quot;) // 设置过滤表前缀</span><br>							.mapperBuilder()<br>							<span class="hljs-comment">// 控制器controller配置</span><br>							.controllerBuilder()<br>							<span class="hljs-comment">// 开启生成@RestController 控制器</span><br>							.enableRestStyle()<br>							<span class="hljs-comment">// 开启驼峰转连字符</span><br>							.enableHyphenStyle()<br>					<span class="hljs-comment">// 开启父类</span><br><span class="hljs-comment">//							.superClass(&quot;com.example.demo.controller.BaseController&quot;)</span><br>							<span class="hljs-comment">// 控制器统一后缀</span><br>							.formatFileName(<span class="hljs-string">&quot;%sController&quot;</span>)<br><br>							<span class="hljs-comment">// service配置</span><br>							.serviceBuilder()<br>							<span class="hljs-comment">// service统一后缀</span><br>							.formatServiceFileName(<span class="hljs-string">&quot;%sService&quot;</span>)<br>							<span class="hljs-comment">// serviceImpl统一后缀</span><br>							.formatServiceImplFileName(<span class="hljs-string">&quot;%sServiceImpl&quot;</span>)<br><br>							<span class="hljs-comment">// mapper配置</span><br>							.mapperBuilder()<br>							<span class="hljs-comment">// 生成基础字段的map映射map</span><br>							.enableBaseResultMap()<br>							<span class="hljs-comment">// 生成基础查询的sql</span><br>							.enableBaseColumnList()<br><br>							<span class="hljs-comment">// 实体类配置</span><br>							.entityBuilder()<br>					<span class="hljs-comment">// 开启链式模型，开启lombok模型不需要开启这个</span><br><span class="hljs-comment">//                            .enableChainModel()</span><br>							<span class="hljs-comment">// 开启lombok模型</span><br>							.enableLombok()<br>							<span class="hljs-comment">// 开启表字段注解</span><br>							.enableTableFieldAnnotation()<br>							<span class="hljs-comment">// 逻辑删除字段</span><br>							<span class="hljs-comment">// TODO TODO</span><br>							.logicDeleteColumnName(<span class="hljs-string">&quot;is_deleted&quot;</span>)<br>							<span class="hljs-comment">// 数据库表映射到实体的命名策略</span><br>							.naming(NamingStrategy.underline_to_camel)<br>					<span class="hljs-comment">// 数据库表字段映射到实体的命名策略,驼峰命名，这个未设置会按照naming来配置</span><br><span class="hljs-comment">//                            .columnNaming(NamingStrategy.underline_to_camel)</span><br>					<span class="hljs-comment">// 指定实体类父类</span><br><span class="hljs-comment">//                            .superClass(&quot;com.mybatisplus.generator.entity.BaseEntity&quot;)</span><br>					<span class="hljs-comment">// 父类字段</span><br><span class="hljs-comment">//                            .addSuperEntityColumns(&quot;id&quot;, &quot;create_id&quot;, &quot;modify_id&quot;, &quot;create_date&quot;, &quot;modify_date&quot;, &quot;is_deleted&quot;)</span><br><span class="hljs-comment">//                             开启 ActiveRecord 模式，即实体类继承Model类,自己提供CRUD操作,不建议使用,会和父类形成单继承冲突</span><br><span class="hljs-comment">//                            .enableActiveRecord()</span><br>							<span class="hljs-comment">// 表字段填充字段，对应数据库字段，插入的时候自动填充</span><br>							.addTableFills(<span class="hljs-keyword">new</span> <span class="hljs-title class_">Column</span>(<span class="hljs-string">&quot;create_date&quot;</span>, FieldFill.INSERT))<br>							<span class="hljs-comment">// 表字段填充字段，对应实体类字段，插入的时候自动填充</span><br>							.addTableFills(<span class="hljs-keyword">new</span> <span class="hljs-title class_">Property</span>(<span class="hljs-string">&quot;createDate&quot;</span>, FieldFill.INSERT))<br>							<span class="hljs-comment">// 表字段填充字段，对应数据库字段，更新的时候自动填充</span><br>							.addTableFills(<span class="hljs-keyword">new</span> <span class="hljs-title class_">Column</span>(<span class="hljs-string">&quot;modify_date&quot;</span>, FieldFill.UPDATE))<br>							<span class="hljs-comment">// 表字段填充字段，对应实体类字段，更新的时候自动填充</span><br>							.addTableFills(<span class="hljs-keyword">new</span> <span class="hljs-title class_">Property</span>(<span class="hljs-string">&quot;modifyDate&quot;</span>, FieldFill.UPDATE))<br>					<span class="hljs-comment">// 忽略的字段</span><br><span class="hljs-comment">//							.addIgnoreColumns(&quot;version&quot;)</span><br>							<span class="hljs-comment">// id自增</span><br>							.idType(IdType.AUTO);<br>					<span class="hljs-comment">// 实体类统一后缀</span><br>				&#125;).templateEngine(<span class="hljs-keyword">new</span> <span class="hljs-title class_">FreemarkerTemplateEngine</span>()) <span class="hljs-comment">// 使用Freemarker引擎模板，默认的是Velocity引擎模板</span><br>				.execute();<br>	&#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<figure class="highlight properties"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br></pre></td><td class="code"><pre><code class="hljs properties"><span class="hljs-comment"># 应用名称 TODO</span><br><span class="hljs-attr">spring.application.name</span>=<span class="hljs-string">demo</span><br><span class="hljs-comment"># 应用服务 WEB 访问端口</span><br><span class="hljs-attr">server.port</span>=<span class="hljs-string">8080</span><br><span class="hljs-comment"># 数据库驱动：</span><br><span class="hljs-attr">spring.datasource.driver-class-name</span>=<span class="hljs-string">com.mysql.cj.jdbc.Driver</span><br><span class="hljs-comment"># 数据源名称</span><br><span class="hljs-attr">spring.datasource.name</span>=<span class="hljs-string">defaultDataSource</span><br><span class="hljs-comment"># 数据库连接地址 TODO</span><br><span class="hljs-attr">spring.datasource.url</span>=<span class="hljs-string">jdbc:mysql://localhost:3306/test?serverTimezone=UTC</span><br><span class="hljs-comment"># 数据库用户名&amp;密码： TODO</span><br><span class="hljs-attr">spring.datasource.username</span>=<span class="hljs-string">root</span><br><span class="hljs-attr">spring.datasource.password</span>=<span class="hljs-string">123456</span><br><span class="hljs-comment"># 配置mybatis-plus 打印sql日志</span><br><span class="hljs-attr">mybatis-plus.configuration.log-impl</span>=<span class="hljs-string">org.apache.ibatis.logging.stdout.StdOutImpl</span><br><span class="hljs-comment"></span><br><span class="hljs-comment"># xml文件路径</span><br><span class="hljs-attr">mybatis-plus.mapper-locations</span>=<span class="hljs-string">classpath:/mapper/**/*.xml</span><br><span class="hljs-comment"></span><br><span class="hljs-comment"># 配置最新全局配置文件</span><br><span class="hljs-comment">#mybatis-plus.config-location=classpath:mybatis-config.xml</span><br><span class="hljs-comment"></span><br><span class="hljs-comment"># 配置mybatis-plus 包路径 TODO</span><br><span class="hljs-attr">mybatis-plus.type-aliases-package</span>=<span class="hljs-string">com.example.demo.domain</span><br><span class="hljs-comment"></span><br><span class="hljs-comment"># mybatis-plus下划线转驼峰配置，默认为true</span><br><span class="hljs-attr">mybatis-plus.configuration.map-underscore-to-camel-case</span>=<span class="hljs-string">true</span><br><span class="hljs-comment"></span><br><span class="hljs-comment"># 配置全局默认主键类型，实体类不用加@TableId(value =&quot;id&quot;,type = IdType.AUTO)</span><br><span class="hljs-attr">mybatis-plus.global-config.db-config.id-type</span>=<span class="hljs-string">auto</span><br><span class="hljs-comment"></span><br><span class="hljs-comment"># 逻辑删除 （1为删除，0为未删除）</span><br><span class="hljs-comment">#mybatis-plus.global-config.db-config.logic-delete-value=1</span><br><span class="hljs-comment">#mybatis-plus.global-config.db-config.logic-not-delete-value=0</span><br><span class="hljs-comment"></span><br><span class="hljs-comment"># 如果java实体类没加注解@TableLogic，则可以配置这个，推介这里配置</span><br><span class="hljs-comment">#mybatis-plus.global-config.db-config.logic-delete-field=is_deleted</span><br></code></pre></td></tr></table></figure>

<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-comment">// 配置分页插件</span><br><span class="hljs-meta">@Configuration</span><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">MyBatisPlusPaginationInnerConfig</span> &#123;<br><br>    <span class="hljs-comment">/**</span><br><span class="hljs-comment">     * 分页插件（官网最新）</span><br><span class="hljs-comment">     */</span><br>    <span class="hljs-meta">@Bean</span><br>    <span class="hljs-keyword">public</span> MybatisPlusInterceptor <span class="hljs-title function_">mybatisPlusInterceptor</span><span class="hljs-params">()</span> &#123;<br>        <span class="hljs-type">MybatisPlusInterceptor</span> <span class="hljs-variable">interceptor</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">MybatisPlusInterceptor</span>();<br>        interceptor.addInnerInterceptor(<span class="hljs-keyword">new</span> <span class="hljs-title class_">PaginationInnerInterceptor</span>(DbType.MYSQL));<br>        <span class="hljs-keyword">return</span> interceptor;<br>    &#125;<br><br>&#125;<br></code></pre></td></tr></table></figure>

<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-comment">// 分页测试</span><br><span class="hljs-meta">@Test</span><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">test1</span><span class="hljs-params">()</span>&#123;<br>    <span class="hljs-comment">// QueryWrapper&lt;User&gt; wrapper = new QueryWrapper&lt;&gt;();</span><br>    <span class="hljs-comment">// wrapper.eq(&quot;weight&quot;,4);</span><br>    <span class="hljs-comment">//第1页，每页2条</span><br>    Page&lt;User&gt; page = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Page</span>&lt;&gt;(<span class="hljs-number">1</span>, <span class="hljs-number">2</span>);<br>    IPage&lt;User&gt; UserIPage = userMapper.selectPage(page, <span class="hljs-literal">null</span>);<br>    System.out.println(<span class="hljs-string">&quot;总条数&quot;</span>+UserIPage.getTotal());<br>    System.out.println(<span class="hljs-string">&quot;总页数&quot;</span>+UserIPage.getPages());<br>    <span class="hljs-comment">//获取当前数据</span><br>    System.out.println(UserIPage.getRecords().toString());<br>&#125;<br></code></pre></td></tr></table></figure>

<h2 id="FastJson"><a href="#FastJson" class="headerlink" title="FastJson"></a>FastJson</h2><p><a target="_blank" rel="noopener" href="https://www.123pan.com/s/tMU0Vv-fFiUd.html">fastjson jar包</a></p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><code class="hljs xml"><span class="hljs-tag">&lt;<span class="hljs-name">dependency</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">groupId</span>&gt;</span>com.alibaba<span class="hljs-tag">&lt;/<span class="hljs-name">groupId</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">artifactId</span>&gt;</span>fastjson<span class="hljs-tag">&lt;/<span class="hljs-name">artifactId</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">version</span>&gt;</span>1.2.72<span class="hljs-tag">&lt;/<span class="hljs-name">version</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">dependency</span>&gt;</span><br></code></pre></td></tr></table></figure>

<blockquote>
<p>用法</p>
</blockquote>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-meta">@Test</span><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">returnJson</span><span class="hljs-params">()</span> &#123;<br>    <span class="hljs-type">User</span> <span class="hljs-variable">user</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">User</span>(<span class="hljs-number">1</span>, <span class="hljs-string">&quot;admin&quot;</span>, <span class="hljs-string">&quot;123456&quot;</span>, <span class="hljs-string">&quot;哈哈&quot;</span>);<br>    System.out.println(<span class="hljs-string">&quot;user1:&quot;</span> + user);<br>    <span class="hljs-comment">// 对象转String</span><br>    System.out.println(<span class="hljs-string">&quot;user2:&quot;</span> + JSON.toJSONString(user));<br>    <span class="hljs-comment">// 集合转json</span><br>    <span class="hljs-type">User</span> <span class="hljs-variable">user1</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">User</span>(<span class="hljs-number">2</span>, <span class="hljs-string">&quot;张三&quot;</span>, <span class="hljs-string">&quot;123456&quot;</span>, <span class="hljs-string">&quot;呵呵&quot;</span>);<br>    <span class="hljs-type">User</span> <span class="hljs-variable">user2</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">User</span>(<span class="hljs-number">2</span>, <span class="hljs-string">&quot;李四&quot;</span>, <span class="hljs-string">&quot;000&quot;</span>, <span class="hljs-string">&quot;呵呵&quot;</span>);<br>    List&lt;User&gt; users = <span class="hljs-keyword">new</span> <span class="hljs-title class_">ArrayList</span>&lt;User&gt;();<br>    users.add(user1);<br>    users.add(user2);<br>    System.out.println(<span class="hljs-string">&quot;集合:&quot;</span> + JSON.toJSONString(users));<br>    <span class="hljs-comment">// 解析json对象</span><br>    System.out.println(<span class="hljs-string">&quot;解析:&quot;</span> + JSON.parseObject(JSON.toJSONString(user)));<br>&#125;<br></code></pre></td></tr></table></figure>

<blockquote>
<p>结果</p>
</blockquote>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><code class="hljs java">user1:com.example.demo.domain.User@5b251fb9<br>user2:&#123;<span class="hljs-string">&quot;id&quot;</span>:<span class="hljs-number">1</span>,<span class="hljs-string">&quot;nickName&quot;</span>:<span class="hljs-string">&quot;哈哈&quot;</span>,<span class="hljs-string">&quot;password&quot;</span>:<span class="hljs-string">&quot;123456&quot;</span>,<span class="hljs-string">&quot;username&quot;</span>:<span class="hljs-string">&quot;admin&quot;</span>&#125;<br>集合:[&#123;<span class="hljs-string">&quot;id&quot;</span>:<span class="hljs-number">2</span>,<span class="hljs-string">&quot;nickName&quot;</span>:<span class="hljs-string">&quot;呵呵&quot;</span>,<span class="hljs-string">&quot;password&quot;</span>:<span class="hljs-string">&quot;123456&quot;</span>,<span class="hljs-string">&quot;username&quot;</span>:<span class="hljs-string">&quot;张三&quot;</span>&#125;,&#123;<span class="hljs-string">&quot;id&quot;</span>:<span class="hljs-number">2</span>,<span class="hljs-string">&quot;nickName&quot;</span>:<span class="hljs-string">&quot;呵呵&quot;</span>,<span class="hljs-string">&quot;password&quot;</span>:<span class="hljs-string">&quot;000&quot;</span>,<span class="hljs-string">&quot;username&quot;</span>:<span class="hljs-string">&quot;李四&quot;</span>&#125;]<br>解析:&#123;<span class="hljs-string">&quot;password&quot;</span>:<span class="hljs-string">&quot;123456&quot;</span>,<span class="hljs-string">&quot;nickName&quot;</span>:<span class="hljs-string">&quot;哈哈&quot;</span>,<span class="hljs-string">&quot;id&quot;</span>:<span class="hljs-number">1</span>,<span class="hljs-string">&quot;username&quot;</span>:<span class="hljs-string">&quot;admin&quot;</span>&#125;<br></code></pre></td></tr></table></figure>


                
              </div>
            
            <hr/>
            <div>
              <div class="post-metas my-3">
  
    <div class="post-meta mr-3 d-flex align-items-center">
      <i class="iconfont icon-category"></i>
      

<span class="category-chains">
  
  
    
      <span class="category-chain">
        
  <a href="/categories/%E5%B8%B8%E7%94%A8%E9%85%8D%E7%BD%AE%E3%80%81%E5%B7%A5%E5%85%B7%E7%B1%BB/" class="category-chain-item">常用配置、工具类</a>
  
  

      </span>
    
  
</span>

    </div>
  
  
    <div class="post-meta">
      <i class="iconfont icon-tags"></i>
      
        <a href="/tags/%E5%B8%B8%E7%94%A8%E9%85%8D%E7%BD%AE%E3%80%81%E5%B7%A5%E5%85%B7%E7%B1%BB/">#常用配置、工具类</a>
      
    </div>
  
</div>


              
  

  <div class="license-box my-3">
    <div class="license-title">
      <div>常用配置、工具类</div>
      <div>http://example.com/2023/03/05/编程语言/Java/学习/基础/008_常用配置、工具类/</div>
    </div>
    <div class="license-meta">
      
        <div class="license-meta-item">
          <div>作者</div>
          <div>深海火锅店</div>
        </div>
      
      
        <div class="license-meta-item license-meta-date">
          <div>发布于</div>
          <div>2023年3月5日</div>
        </div>
      
      
      
        <div class="license-meta-item">
          <div>许可协议</div>
          <div>
            
              
              
                <a target="_blank" href="https://creativecommons.org/licenses/by/4.0/">
                  <span class="hint--top hint--rounded" aria-label="BY - 署名">
                    <i class="iconfont icon-by"></i>
                  </span>
                </a>
              
            
          </div>
        </div>
      
    </div>
    <div class="license-icon iconfont"></div>
  </div>



              
                <div class="post-prevnext my-3">
                  <article class="post-prev col-6">
                    
                    
                      <a href="/2023/03/10/%E7%BC%96%E7%A8%8B%E8%AF%AD%E8%A8%80/Java/%E5%AD%A6%E4%B9%A0/Oracle/01_Jdbc%E8%BF%9E%E6%8E%A5oracle/" title="JDBC连接Oracle实现CRUD操作">
                        <i class="iconfont icon-arrowleft"></i>
                        <span class="hidden-mobile">JDBC连接Oracle实现CRUD操作</span>
                        <span class="visible-mobile">上一篇</span>
                      </a>
                    
                  </article>
                  <article class="post-next col-6">
                    
                    
                      <a href="/2023/03/05/%E7%BC%96%E7%A8%8B%E8%AF%AD%E8%A8%80/Java/%E5%AD%A6%E4%B9%A0/%E5%9F%BA%E7%A1%80/007_Optional%E4%BD%BF%E7%94%A8/" title="Optional使用">
                        <span class="hidden-mobile">Optional使用</span>
                        <span class="visible-mobile">下一篇</span>
                        <i class="iconfont icon-arrowright"></i>
                      </a>
                    
                  </article>
                </div>
              
            </div>

            
          </article>
        </div>
      </div>
    </div>

    <div class="side-col d-none d-lg-block col-lg-2">
      
  <aside class="sidebar" style="margin-left: -1rem">
    <div id="toc">
  <p class="toc-header"><i class="iconfont icon-list"></i>&nbsp;目录</p>
  <div class="toc-body" id="toc-body"></div>
</div>



  </aside>


    </div>
  </div>
</div>





  



  



  



  



  







    

    
      <a id="scroll-top-button" aria-label="TOP" href="#" role="button">
        <i class="iconfont icon-arrowup" aria-hidden="true"></i>
      </a>
    

    
      <div class="modal fade" id="modalSearch" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
     aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header text-center">
        <h4 class="modal-title w-100 font-weight-bold">搜索</h4>
        <button type="button" id="local-search-close" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body mx-3">
        <div class="md-form mb-5">
          <input type="text" id="local-search-input" class="form-control validate">
          <label data-error="x" data-success="v" for="local-search-input">关键词</label>
        </div>
        <div class="list-group" id="local-search-result"></div>
      </div>
    </div>
  </div>
</div>

    

    
  </main>

  <footer>
    <div class="footer-inner">
  
    <div class="footer-content">
       <a href="https://hexo.io" target="_blank" rel="nofollow noopener"><span>Hexo</span></a> <i class="iconfont icon-love"></i> <a href="https://github.com/fluid-dev/hexo-theme-fluid" target="_blank" rel="nofollow noopener"><span>Fluid</span></a> 
    </div>
  
  
    <div class="statistics">
  
  

  
    
      <span id="busuanzi_container_site_pv" style="display: none">
        总访问量 
        <span id="busuanzi_value_site_pv"></span>
         次
      </span>
    
    
      <span id="busuanzi_container_site_uv" style="display: none">
        总访客数 
        <span id="busuanzi_value_site_uv"></span>
         人
      </span>
    
    
  
</div>

  
  
  
</div>

  </footer>

  <!-- Scripts -->
  
  <script  src="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.js" ></script>
  <link  rel="stylesheet" href="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.css" />

  <script>
    NProgress.configure({"showSpinner":false,"trickleSpeed":100})
    NProgress.start()
    window.addEventListener('load', function() {
      NProgress.done();
    })
  </script>


<script  src="https://lib.baomitu.com/jquery/3.6.0/jquery.min.js" ></script>
<script  src="https://lib.baomitu.com/twitter-bootstrap/4.6.1/js/bootstrap.min.js" ></script>
<script  src="/js/events.js" ></script>
<script  src="/js/plugins.js" ></script>


  <script  src="https://lib.baomitu.com/typed.js/2.0.12/typed.min.js" ></script>
  <script>
    (function (window, document) {
      var typing = Fluid.plugins.typing;
      var subtitle = document.getElementById('subtitle');
      if (!subtitle || !typing) {
        return;
      }
      var text = subtitle.getAttribute('data-typed-text');
      
        typing(text);
      
    })(window, document);
  </script>




  
    <script  src="/js/img-lazyload.js" ></script>
  




  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/tocbot/4.18.2/tocbot.min.js', function() {
    var toc = jQuery('#toc');
    if (toc.length === 0 || !window.tocbot) { return; }
    var boardCtn = jQuery('#board-ctn');
    var boardTop = boardCtn.offset().top;

    window.tocbot.init(Object.assign({
      tocSelector     : '#toc-body',
      contentSelector : '.markdown-body',
      linkClass       : 'tocbot-link',
      activeLinkClass : 'tocbot-active-link',
      listClass       : 'tocbot-list',
      isCollapsedClass: 'tocbot-is-collapsed',
      collapsibleClass: 'tocbot-is-collapsible',
      scrollSmooth    : true,
      includeTitleTags: true,
      headingsOffset  : -boardTop,
    }, CONFIG.toc));
    if (toc.find('.toc-list-item').length > 0) {
      toc.css('visibility', 'visible');
    }

    Fluid.events.registerRefreshCallback(function() {
      if ('tocbot' in window) {
        tocbot.refresh();
        var toc = jQuery('#toc');
        if (toc.length === 0 || !tocbot) {
          return;
        }
        if (toc.find('.toc-list-item').length > 0) {
          toc.css('visibility', 'visible');
        }
      }
    });
  });
</script>


  <script src=https://lib.baomitu.com/clipboard.js/2.0.11/clipboard.min.js></script>

  <script>Fluid.plugins.codeWidget();</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/anchor-js/4.3.1/anchor.min.js', function() {
    window.anchors.options = {
      placement: CONFIG.anchorjs.placement,
      visible  : CONFIG.anchorjs.visible
    };
    if (CONFIG.anchorjs.icon) {
      window.anchors.options.icon = CONFIG.anchorjs.icon;
    }
    var el = (CONFIG.anchorjs.element || 'h1,h2,h3,h4,h5,h6').split(',');
    var res = [];
    for (var item of el) {
      res.push('.markdown-body > ' + item.trim());
    }
    if (CONFIG.anchorjs.placement === 'left') {
      window.anchors.options.class = 'anchorjs-link-left';
    }
    window.anchors.add(res.join(', '));

    Fluid.events.registerRefreshCallback(function() {
      if ('anchors' in window) {
        anchors.removeAll();
        var el = (CONFIG.anchorjs.element || 'h1,h2,h3,h4,h5,h6').split(',');
        var res = [];
        for (var item of el) {
          res.push('.markdown-body > ' + item.trim());
        }
        if (CONFIG.anchorjs.placement === 'left') {
          anchors.options.class = 'anchorjs-link-left';
        }
        anchors.add(res.join(', '));
      }
    });
  });
</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.js', function() {
    Fluid.plugins.fancyBox();
  });
</script>


  <script>Fluid.plugins.imageCaption();</script>

  <script  src="/js/local-search.js" ></script>

  <script defer src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js" ></script>




  
<script src="//cdn.jsdelivr.net/gh/EmoryHuang/BlogBeautify@1.1/DynamicRibbon.min.js"></script>



<!-- 主题的启动项，将它保持在最底部 -->
<!-- the boot of the theme, keep it at the bottom -->
<script  src="/js/boot.js" ></script>


  

  <noscript>
    <div class="noscript-warning">博客在允许 JavaScript 运行的环境下浏览效果更佳</div>
  </noscript>
</body>
</html>
